Click here to Skip to main content
14,937,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Server File Obj: (datafile Already on the server) { "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`)
    );'/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 !!') })

What I have tried:

Server Post Obj: (req.body) {"Show": {"Cars": {"Name": "Audi TT"}}}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900