when i'm trying to delete an item by FindByIdAndDelete it doesn't delete anything. when i make the delete request in postman i don't get no errors just the item's in the db. happens the same with my put request.
that's my code:
router.delete("/", (req, res) => {
Appointment.find({}, (err, data) => {
if (err) {
return res.status(500).json();
} else {
return res.json(data);
}
});
});
router.delete("/:id", (req, res) => {
const id = req.params.id;
Appointment.findByIdAndDelete(id, (err, data) => {
if (err) {
return res.status(500).json();
} else {
return res.json(data);
}
});
});
that's the body i put in my request :
{
"id": "5e3ef4950e1b4027201e73bf"
}
what am i doing wrong?
findByIdAndDelete
doesn't throw exception when it doesn't find the document to be deleted. You need to check if data is null or not.
Also your first route must be a GET route instead of DELETE.
router.get("/", (req, res) => {
Appointment.find({}, (err, data) => {
if (err) {
return res.status(500).json();
} else {
return res.json(data);
}
});
});
router.delete("/:id", (req, res) => {
const id = req.params.id;
Appointment.findByIdAndDelete(id, (err, data) => {
if (err) {
return res.status(500).json();
} else {
if (data) {
return res.json(data);
} else {
return res.status(400).send("Document not found, check id");
}
}
});
});