Node.js-FindOneAndUpdate方法有效,但是使我的网站在events.js上崩溃(使用mongo)

时间:2019-07-15 22:09:35

标签: arrays node.js mongodb

我想删除具有从前端获得的ID的数组索引。我的代码工作正常,元素已删除,但是网站崩溃了,我必须重新启动npm或检查mongo的地图集才能检查结果。我真的在黑暗中。 这是错误:

 events.js:167
          throw er; // Unhandled 'error' event
          ^

    ReferenceError: errors is not defined
        at User.findOneAndUpdate (C:\repos\InventoryApp\routes\index.js:52:17)
        at C:\repos\InventoryApp\node_modules\mongoose\lib\model.js:4851:16
        at process.nextTick (C:\repos\InventoryApp\node_modules\mongoose\lib\query.js:2776:28)
        at process._tickCallback (internal/process/next_tick.js:61:11)
    Emitted 'error' event at:
        at C:\repos\InventoryApp\node_modules\mongoose\lib\model.js:4853:13
        at process.nextTick (C:\repos\InventoryApp\node_modules\mongoose\lib\query.js:2776:28)
        at process._tickCallback (internal/process/next_tick.js:61:11)
    [nodemon] app crashed - waiting for file changes before starting...

//all variables are set and I have done a push method with the same variables
User.findOneAndUpdate(
        { email : useremail },                  
        { $pull: { products: {
            _id : mongoose.Types.ObjectId(id)
        }  } },
        (err) => {
             if (err) console.log('found errors');
                req.flash('success_msg', 'New record');

    })

1 个答案:

答案 0 :(得分:0)

我发现mongoose.Types.ObjectId(id)是造成此问题的原因。 通过进行_id : id,我努力使其发挥作用。