0

I have a file called data that initializes a class using a list. I'm wondering if it is possible to add data to this file automatically from another file by importing the data.js file. Is that possible ? If so how would i go about it. Because I tried using .push() and .concat() from my app.js file but I don't see it update in the data.js file.

The Data.js file

export const CATEGORIES = [
    new Catergory('My Recipies', 'My Recipies',myRecipies),
    new Catergory('Italian', 'Italian', italian),
    new Catergory('Indian', 'Indian',indian),
    new Catergory('Western', 'Western',western),
    new Catergory('Quick n Easy', 'Quick n Easy',quick),
    new Catergory('Desert', 'Desert',desert),
    new Catergory('Breakfast', 'Breakfast',breakfast),        
]

App.js File

import data from './Data/categories/data'
import Catergory from './Data/categories/Catergory'

data.push(
    new Catergory('Drinks',' My Recipies', myRecipies)
)

I would really appreciate it from the bottom of my heart if someone could help me add this app.js code to my external data.js. Thank you in advance!!!!!

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
Dilhan Bhagat
  • 408
  • 1
  • 10
  • 20

2 Answers2

2

No.

If you want to change the file, then you need to explicitly write to the file, and you can't do that from JavaScript running in the web browser. (It would be a terrible security problem if web browsers could write to any file they liked on the server!).

This problem is better solved by writing a web service (written in whatever server-side language you like, which could be JavaScript via the Express framework), backed by a database (maybe SQLite if you want to keep things really simple) which you can read to and write from using Ajax.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

JavaScript running in the browser isn't allowed to modify local files, for security reasons. See this question for how you can make this work if you just want to run your app locally, on your own device (not in the browser). If you are deploying your app on the web from a server, then you can use php or Ajax to update the files on the server. See this question for an explanation of how you would go about modifying a JSON file on the server with php. Speaking of which, you might want to change your file to JSON so it's a bit easier to work with. Hopefully some of these links will point you in the right direction, depending on your use case.

Leaf the Legend
  • 471
  • 3
  • 9