I have read some other questions like this, but nobody has the right solution to my problem.
I have an empty a json file named apartments.json:
[]
I want to store a list of apartments in it. I retrieve an apartment from a post request and I'm using node express (v8.10). This is the code:
router.post('/', (req, res, next) => {
let apa = {
id: req.body.id,
address: req.body.address
}
let apartments = require('path/to/json');
apartments.push(apa);
let str = JSON.stringify(apartments);
console.log(str);
)};
I want to store back the new content to apartments.json after updated, the problem is that stringify() returns [object Object]. I have tried to stringify apartments and apa to test it, but it gives me always the same thing
echo '[]' > t.json; echo "let a = require('./t.json'); a.push({'a':'b'}); console.log(JSON.stringify(a)); > t.jsthen run it.node t.js, results in:[{"a":"b"}]