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
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.
Import the Json file at the top of your code file
import data from ./data.json
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With