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!!!!!

3
  • Do you mean modifying the file itself? Commented Jul 12, 2020 at 12:10
  • yes adding items to the CATEGORIES array from another the App.js file. Commented Jul 12, 2020 at 12:11
  • Let me clarify it. Do you want the Data.js file to be modified permanently or do you just want to use CATEGORIES array with the data you push in App.js file? Commented Jul 12, 2020 at 12:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.