首页 > 解决方案 > .findByIdAndRemove 不是函数

问题描述

考虑以下代码:

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
                }

            })

});

我收到一条错误消息,上面写着“.findByIdAndRemove 不是函数”。

我怎样才能防止这个错误?

标签: node.jsmongodbmongoose

解决方案


在 mongoDB_id中,为数据库中特定项目的 id保留关键字。我相信你应该改变 id你的_id代码。例如:

user.findByIdAndRemove({_id :idRemove},(err, doc) =>

推荐阅读