5

Or actually the question should be

How to write data to JSON file using Reactjs and Nodejs

I'm very first to react and do not understand which database should I use with.

PS. It's can easily to read json file with

var data = require('./data.json');

but to write data I try using npm 'jsonfile' package and both try to used Nodejs and got this error

TypeError: fs.writeFileSync is not a function

2
  • 1
    Could you include the complete code? Commented Jul 7, 2017 at 9:14
  • 1
    If you're trying to access your filesystem/database directly from your front-end React app, that's not going to work. You need to communicate with a server via HTTP, and then have the server do the stuff that interacts with the files/database. Commented Jul 7, 2017 at 9:14

1 Answer 1

1

I was literally searching for an answer on this today. I'm using React so it should be similiar for you. I think I figured it out.

I found you have to do two things.

  1. Import the Json file at the top of your code file

    import data from ./data.json

  2. Declare a variable passing the name of the variable specified in the import statement(in this case data)

    const data = require('./data.json');

Now calling {data} should allow you to usae the data of the file

Hope this helps :D

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

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.