node.js - Mongoose 错误 : Cannot update __v and __v at the same time

标签 node.js mongodb mongoose

我需要这方面的帮助。 我有这个从一开始就一直在工作的 Node 项目。 最近我开始收到关于 mongoose 无法同时更新 __v 和 __v 的错误(详情如下) 我的第一个想法是 Mongoose 的新更新带来了这个,但我不确定。 任何帮助将不胜感激。 谢谢

/.../node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
MongoError: exception: Cannot update '__v' and '__v' at the same time
    at Object.toError (/.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
    at /.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1131:31
    at /.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1846:9
    at Server.Base._callHandler (/.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:445:41)
    at /.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:478:18
    at MongoReply.parseBody (/.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:436:20)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (/.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)
    at EventEmitter.emit (events.js:98:17)

编辑 看起来当我尝试保存时抛出了错误。这是我调用的保存方法;

save: function (callback) {
  // A Helper function
  function setID(instance, id) {
    instance._id = id;
  };

  var self = this; // Preserve reference to the instance
  var update = Utils.clone(this);
  delete update._id; // Delete the _id property, otherwise Mongo will return a "Mod on _id not allowed" error

  // Prepare updates for saving
  for (var key in update) {
    if (update[key] == undefined)
      delete update[key];
    if (key == 'company' || key == 'local_currency')
      update[key] = update[key].getID();
  }

  PreferenceModel.save(this._id, update, function (err, savedDoc) {
    if (err) callback(err);
    else {
      if (!self.getID()) // If the object didn't previously have an _id property
        setID(self, savedDoc._id);
      callback(null, self);
    }
  });
}

这就是我所说的;

preference.save(function (err, savedPreference) {
  if (err) callback(err);
  else callback(null);
});

还有,这里是 PreferenceModel.save 方法;

function save(id, update, callback) {
  Preference.findByIdAndUpdate(id, update, {upsert: true}, function (err, savedDoc) {
    if (err) callback(err);
    else callback(null, savedDoc);
  });
}

最佳答案

我建议你将_id相关的删除逻辑放到你的mongoose模型Schema定义文件中:

var UserSchema = new mongoose.Schema(fieldDefinitions);

// Ensure virtual fields are serialised.
UserSchema.set('toJSON', {
    virtuals: true
});

// Ensure able to see virtual fields output when using console.log(obj)
UserSchema.set('toObject', {
    virtuals: true
});

UserSchema.options.toJSON = {

    transform : function(doc, ret, options) {

        console.log('--> ' + require('util').inspect( ret._id.id ));

        ret.id = ret._id.id;
        delete ret._id;
        delete ret.__v;

        return ret;
    },
    virtuals: true
};

然后在你的回调中执行 toJSON :

var processedJson = resultDoc.toJSON();

检索处理后的版本,很好地隐藏了可重用的逻辑。
注意:toJSON() 也被 JSON.stringify() 神奇地执行

关于node.js - Mongoose 错误 : Cannot update __v and __v at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879111/

相关文章:

node.js - DialogFlow v2 错误 : Resource name '' does not match 'projects/*/locations/*/agent/environments/*/users/*/sessions/*'

node.js - 如何为 Web 应用程序运行 Node.JS 服务器?

mongodb - 为什么我的查询没有更新数组中的对象?

node.js - 如何在 Mongoose 中填充对象的嵌入数组

node.js - 当我使用带有 .stream() 的 mongo tailable 光标时,我的 CPU 过热

Node.js 和 ACL

javascript - Node.js、Vue.js 和 Passport.js。 .isAuthenticated() 总是返回 false?可能是 Axios header ?

node.js - 使用 Postman 测试具有参数的 post API 调用

node.js - 插入 Mongoose 后找不到文档

python - mongodb游标id无效错误