I am sending a post request to node server that contains a JSON object with a value. Hoping to update a certain value, of an Object in a JSON file (no DB just experimental). I just cannot figure out how, I hope the code will be self-explanatory I put all the examples there
Post Sent: Post structure coming form front-end const dataToSend = JSON.stringify({ "Show": {"Cars":{"Name": "Audi TT"}}});
Server File Obj: (datafile) { "Show": {"Cars": {"Name": "Audi A4","MaxSpeed": 260,"Color": "Blue"}}}
Server Post Obj: (req.body) {"Show": {"Cars": {"Name": "Audi TT"}}}
Problem is my current Code Returns: {"Show": {"Cars": {"Name": "Audi TT"}}} And removes "MaxSpeed": 260,"Color": "Blue" from the obj. Thanks for your effort in advance.
const dataFile = JSON.parse(fs.readFileSync(`${__dirname}/../Data/Data.json`) ); router.post('/app', (req, res, next) => { // Update coming from Front-End let post = req.body let mutatedObj = Object.assign(dataFile, post) fs.writeFile(`${__dirname}/../Data/Data.json`, JSON.stringify(mutatedObj), err => { res.send('we got your request !!') }) }
没有评论:
发表评论