Mongoose findByIdAndRemove()抛出错误

时间:2017-06-20 13:40:18

标签: javascript node.js mongodb mongoose backend

在我的后端我想删除一个文件,在我的MongoDB数据库中用它的ID标记。因此我使用以下代码。但不是删除它,而是抛出一个错误,它无法设置属性' _id'未定义的

代码:

Seller.findByIdAndRemove("594801105cb8571868f9cd55",function (err) {
    console.log(err);
});

错误:

TypeError: Cannot set property '_id' of undefined
    at resetId (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\schema\objectid.js:204:16)
    at ObjectId.SchemaType._applySetters (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\schematype.js:637:22)
    at ObjectId.SchemaType.applySetters (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\schematype.js:661:16)
    at ObjectId.SchemaType._castForQuery (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\schematype.js:1021:17)
    at ObjectId.castForQuery (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\schema\objectid.js:189:15)
    at cast (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\cast.js:232:32)
    at model.Query.Query.cast (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\query.js:2933:12)
    at castQuery (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\query.js:2835:18)
    at model.Query.Query._findAndModify (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\query.js:1980:17)
    at model.Query.Query.findOneAndRemove (Z:\Documents\Programming\Projext XY\repo\node_modules\mquery\lib\mquery.js:2585:15)
    at model.Query.Query._findOneAndRemove (Z:\Documents\Programming\Projext XY\repo\node_modules\mongoose\lib\query.js:1944:31)
    at Z:\Documents\Programming\Projext XY\repo\node_modules\kareem\index.js:257:8
    at Z:\Documents\Programming\Projext XY\repo\node_modules\kareem\index.js:23:7
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

1 个答案:

答案 0 :(得分:0)

经过几个小时的搜索,我找到了解决方案。问题是,在我使用的mongoose版本中,每当选项出现时都会出现此错误:

runSettersOnQuery: true

已在Schema上设置。随着Github上的最新版本问题得以解决