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`)
);
router.post('/app', (req, res, next) => {
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"}}}