繁体   English   中英

猫鼬删除错误对象没有方法删除

[英]Mongoose Remove Error Object has no method remove

我尝试查找并删除一个项目。

这是我的代码,很简单:

playlistItem.find( { $and: [ { songId : data.songId }, { userAdded : data.userAdded } , { playlistId : data.playlistId } ] } ).limit(1).lean().exec(function(err, item){
                            if (!item) {
                                    callback(null,{message:"playlist item not found"})
                            }else{
                                // delete playlist item
                                  item.remove(function(err) {
                                    if (err){
                                        callback( new Error('new error') );
                                    }else{
                                        callback(null,{message:"playlist successfully removed"})
                                    } 


                                  });   
                            }
                        })

我想先找到我的物品,然后将其删除

当我运行此代码时,出现此错误:

item.remove(function(err) {
                                                                       ^
TypeError: Object [object Object] has no method 'remove'

我想知道我错了。

感谢您的帮助。

我假设您要删除该对象。 显然,该模型没有直接删除它的方法。

由于您使用的是Mongoose,因此这是从数据库中删除对象的查询:

Model.remove({ id: X }, function (err) {
  if (err) return handleError(err);
  // removed!
});

因此,在您的情况下,我认为这样的事情应该起作用:

PlayListItems.remove({ id: item.id }, function (err) {
  if (err) return handleError(err);
  // removed!
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM