【问题标题】:Mongoose .save function returning 'No matching document found for id "32fdj..." version 0 modifiedPaths "cmRooms"'Mongoose .save 函数返回“未找到 id "32fdj..." 版本 0 modifiedPaths "cmRooms" 的匹配文档'
【发布时间】:2020-05-18 02:24:41
【问题描述】:
    let property = await PropertyModel.findOne({
      propertyId: res.locals.userInformations.userId
    });

    let cmRooms = property.cmRooms;
    const newCmRooms = cmRooms.filter(
      cmRoom => cmRoom.cmRoomId !== "1234";
    );

    property.cmRooms = newCmRooms;
    await property.save(err => {
      if (err) console.log(err);
    });

我收到以下错误

message: '没有找到与 id 匹配的文档 "5eb7638320ab5d0633477081" 版本 0 修改路径 "cmRooms"',

名称:'VersionError',版本:0,

修改路径:['cmRooms']}

【问题讨论】:

  • 1) findOne 的右括号丢失 2) 我相信您不能在模式上使用 findOne。您必须使用 - let Prop = mongoose.model('Property',PropertySchema); 制作模型Prop.findOne({...})
  • 实际上是模型,错误的命名约定!我可以找到所有属性,
  • 让属性 = 等待 PropertyModel.findOne({ propertyId: res.locals.userInformations.userId }).exec(); //注意.exec()
  • 这也没用!

标签: node.js mongoose


【解决方案1】:

能不能把上面的代码改成这个试试:

await PropertyModel.findOne({
    propertyId: res.locals.userInformations.userId
}, async function (err, property) {
    if (!err) {
        let cmRooms = property.cmRooms;
        const newCmRooms = cmRooms.filter(
            cmRoom => cmRoom.cmRoomId !== "1234"
        );

        property.cmRooms = newCmRooms;
        await property.save(err => {
            if (err) console.log(err);
        });
    }
});

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 2018-02-27
    • 2017-12-26
    • 2019-07-14
    • 2013-07-04
    • 2016-07-30
    • 2018-05-12
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多