I am trying to add a unique ID property to each object in an array after it has been submitted from an input & text area. When I console.log the array as I add more entries it looks like so:
[{"title":"hello","text":"sir"}]
I have two const variables I'm using currently. The contents of the note array get written in to a db.json file.
const notes = [];
const newID = 0;
This is an express js program and below is the function I have to write the input data to the appropriate file. I would like to add an ID for each entry preferably in this function.
app.post("/api/notes", (req, res) => {
let newNote = req.body;
notes.push(newNote)
fs.writeFile(path.join(__dirname, 'db/db.json'), JSON.stringify(notes), (err) => {
if (err) throw err;
});
res.end();
});
I've tried to map the new property unsuccessfully I've also tried a forEach to run through the array to add the id++.
Please let me know if there is any pertinent information I am missing here. Thanks in advance for any help/feedback!