【问题标题】:ERROR { MongoError: Unknown modifier: $pushAll when updating with mongooseERROR { MongoError: Unknown modifier: $pushAll 使用 mongoose 更新时
【发布时间】:2020-02-26 00:58:00
【问题描述】:

我正在将 AngularJS 应用程序转换为 Angular 7,它们共享一个后端 API。我正在提交相同的数据,并且记录正确,但是在尝试保存记录时,我在后端返回了一个关于未知修饰符 $pushAll 的错误。

我的控制器代码:

    Volunteer
    .findById(volunteerId)
    .select('shifts')
    .exec(function(err, volunteer) {
      var thisShift;
      var response = {
        status : 200,
        message : {}
      };
      if (err) {
        console.log("Error finding volunteer request");
        response.status = 500;
        response.message = err;
      } else if(!volunteer) {
        console.log("Volunteer request not found", volunteerId);
        response.status = 404;
        response.message = {
          "message" : "Volunteer request not found " + volunteerId
        };
      } else {
        // Get the shift
        thisShift = volunteer.shifts.id(shiftId);
        console.log("SHIFT BEFORE UPDATE", JSON.stringify(thisShift));
        // If the shift doesn't exist Mongoose returns null
        if (!thisShift) {
          response.status = 404;
          response.message = {
            "message" : "Shift not found " + shiftId
          };
        }
      }
      if (response.status !== 200) {
        res
          .status(response.status)
          .json(response.message);
      } else {
        thisShift.volleyteers.push({
          fullname: user.name,
          phone: user.phone,
          email: user.username
        });
        console.log("thisShift to be saved", JSON.stringify(thisShift));
        volunteer.save(function(err, volunteerUpdated) {
          if (err) {
            console.log("ERROR",err);
            res
              .status(500)
              .json(err);
          } else {
            console.log("SUCCESS!");
            res
              .status(204)
              .json({message: "Thank you for volunteering!  You can see where you need to on your profile page or someone will contact you."});
          }
        });
      }
    });
};

我得到的错误以及后端的 console.logged 内容是:

我正在使用:

  • 节点 11.13.0
  • MongoDB服务器版本:4.0.8
  • “猫鼬”:“^4.9.6”

如何让这个错误消失,以便保存更新的记录?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我遇到了同样的问题。问题是 $pushall 函数已被弃用,但一些旧版本的 Mongoose 仍在使用它。我将 Mongoose 升级到 5.6.11 版本(最新版本),问题消失了。

    P/S:如果你使用 mongoose-unique-validator 插件,你也应该升级它(我升级到版本 2.0.3)

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      这个issue comes to mind$pullAll 在 MongoDB 中已弃用,但 mongoose 在幕后使用它。

      解决方法似乎是将usePushEach: true 添加到架构中,直到版本 5.0.0-rc2 根据this

      这段代码似乎是它的原因:

      thisShift.volleyteers.push({
         fullname: user.name,
         phone: user.phone,
         email: user.username
      });
      

      因为您要求 mongoose 保存一组对象。

      猫鼬不在幕后使用$pushAll 的另一种解决方法似乎是尝试使用 concat:

      thisShift.volleyteers.concat([{
         fullname: user.name,
         phone: user.phone,
         email: user.username
      }]);
      

      作为specified here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-16
        • 2020-05-05
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        相关资源
        最近更新 更多