Stack Overflow Asked by thomas bouasli on January 3, 2022
I have an JSON file that holds an array of objects, like so:
[
{ "id": 1, "name": "JavaScript Begginer", "duration": "10 Hours" },
{ "id": 2, "name": "JavaScript Intermediate", "duration": "15 Hours" },
{ "id": 3, "name": "JavaScript Advanced", "duration": "20 Hours" }
]
and I wish to add a new object to it using Node, can this be accomplished with the File System Module?
Here is where I want the change to be made:
app.post("/api/courses", (req, res) => {
const course = {
id: courses.length + 1,
name: req.body.name,
};
res.send(course);
courses.json.push(course); //Something like this
});
I did try fs.appendFile but it returns this:
[
{ "id": 1, "name": "JavaScript Begginer", "duration": "10 Hours" },
{ "id": 2, "name": "JavaScript Intermediate", "duration": "15 Hours" },
{ "id": 3, "name": "JavaScript Advanced", "duration": "20 Hours" }
]
[object Object]
Check out lowdb... its a fs based wrapper for working with json file as your persisted storage method:
Answered by jremi on January 3, 2022
3 Asked on January 5, 2022 by armel-tedjou
1 Asked on January 5, 2022 by alejandra-dip
1 Asked on January 5, 2022 by aweakprogrammer
12 Asked on January 5, 2022 by manish-mahajan
1 Asked on January 5, 2022 by juan-esteban-rios-gonzalez
3 Asked on January 5, 2022 by jeremiah-williams
2 Asked on January 5, 2022 by user12514433
1 Asked on January 5, 2022
3 Asked on January 5, 2022 by jane-sully
conv neural network keras keras layer loss function tensorflow
4 Asked on January 5, 2022
1 Asked on January 5, 2022
4 Asked on January 5, 2022 by user12097954
1 Asked on January 5, 2022 by franco-milanese
2 Asked on January 5, 2022 by vzdiegocm
1 Asked on January 5, 2022 by basit-anlat
1 Asked on January 5, 2022
1 Asked on January 5, 2022
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP