-2
When accessing this route that deletes an item:
app.get("/excluir/:id/:categoria", (req, res) => {
let dados = JSON.parse(fs.readFileSync("./data/dados.json", "utf8"));
var id = req.params.id;
var categoria = req.params.categoria;
console.log(id, categoria);
for (var i = 0; i < dados.length; i++) {
if (dados[i].id == id && dados[i].categoria) {
delete dados[i];
}
}
dados = dados.filter(function (el) {
return el != null;
});
fs.writeFile("./data/dados.json", JSON.stringify(dados, null, 2), function (
err
) {
if (!err) return res.send("Write file error!");
});
console.log(dados);
res.redirect("/cardapio");
});
My browser looks like this:
But if I reload the tab it works perfectly.
Friend, remove the first image and enter the code. Then select the code and press Ctrl+K
– Evilmaax
This error is not related to your code, but rather that the server on localhost is not running. Happens in the same session only with this route, the others work normally?
– Ademir Mazer Jr - Nuno
I realized that the problem is in the part of the writing in the file, it is not waiting to write in the file to then run the next line, why when I update the right, because it gave time to finish the execution.
– Ph4ra0hX