mongoose .findByIdAndRemove不是函数

ibps3vxo  于 5个月前  发布在  Go
关注(0)|答案(2)|浏览(71)

下面是一个例子:

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不是一个函数”。
我如何防止这种错误?

klh5stk1

klh5stk11#

mongoDB中_id是数据库中特定项id的保留关键字,我认为您应该在代码中将**id改为_id,例如:

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

字符串

yqkkidmi

yqkkidmi2#

查看文档https://mongoosejs.com/docs/api.html
findByIdAndRemove不在原型中,我认为这意味着他们打算让你直接访问模型:
例如

const Example = mongoose.model('Example',
  new mongoose.Schema({
    description: String
  })
);

Example.findByIdAndRemove({}, (err, doc) => {})

字符串
这是有意义的,因为使用特定的DTO示例来访问/修改不同的示例会有点尴尬。

相关问题