Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.findByIdAndRemove is not a function

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?

like image 985
Chirag Mittal Avatar asked Mar 03 '26 02:03

Chirag Mittal


1 Answers

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:

like image 153
Jone Smith Avatar answered Mar 04 '26 14:03

Jone Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!