Anytime I make a delete request to my node js server, it can only delete one item from my json file between server starts. If I try to make a second delete request, it works, but it also rewrites back the last item I deleted.
Here is the node js delete implementation using express:
const todos = require("./todos.json"); //Load the node js File
const fs = require("fs");
app.delete("/api/:id", (req, res) => {
const id = req.params.id;
let newTodos = todos.filter((todo) => {
return todo.id !== id;
});
fs.writeFileSync(__dirname + "/todos.json", JSON.stringify(newTodos));
todos = newTodos;
res.send("Todo deleted");
});
This is how the items in the json file look they are contained in a regualar array:
{
"id": "bd58d991-bdda-4d8d-aca3-ae7ee01e1296",
"activity": "Buy Groceries",
"completed": false
}
newTodosvariable filled?todoswith the new content. it's always starting with the original file contents when you call .delete.