This is constantly giving me error that Contact.remove() is not a function. I want to delete a particular contact by passing an id.
const DeleteContact = asyncHandler(async (req, res) => {
const contact = await Contact.findById(req.params.id);
if (!contact) {
res.status(404)
throw new Error("Contact not found");
}
await Contact.remove();
res.status(200).json(contact);
});
2
Answers
I would do something like this:
Your error is probably because of using
delete
instead ofdeleteOne
, i think they recently had a change on their function namesYou should use
Model.findByIdAndDelete()
if you know the_id
of the document to would like to delete like so:Note: the
try/catch
may be unnecessary if yourasyncHandler
is well written but I did not want to assume.