Consider the following code:
app.delete('/customer/:id',(req,res) =>{
var idRemove = String(req.params.id);
console.log(idRemove);//this part is working
var user = new Customers(req.body);
console.log(user)//this part is working
user.findByIdAndRemove({id :idRemove},(err, doc) => {
if (!err)
res.status(200).send(doc);
else {
res.status(500).send(err)
//showing error here telling me that user.findByIdAndRemove is not a function
}
})
});
I am receiving an error that says ".findByIdAndRemove is not a function."
How can I prevent this error?
The error message indicates that findByIdAndRemove is not a function, which suggests that the Mongoose model might not have the method findByIdAndRemove. Indeed, as of Mongoose version 5.0.0 and later, findByIdAndRemove has been deprecated.
To fix this issue, you can use findByIdAndDelete instead. Modify the relevant line in your code like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With