猫鼬错误:无法同时更新__v和__v [英] Mongoose error: Cannot update __v and __v at the same time

查看:84
本文介绍了猫鼬错误:无法同时更新__v和__v的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助. 我有一个从一开始就在工作的节点项目. 最近,我开始收到关于猫鼬无法同时更新__v和__v的错误(详细信息如下) 我首先想到的是对猫鼬的新更新带来了这一点,但是我不确定. 任何帮助将不胜感激. 谢谢

/.../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方法;

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相关的删除逻辑放入猫鼬模型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()神奇地执行.

I need help with this. I have this node project that's been working from the beginning. Recently I began getting an error about mongoose not being able to update __v an __v at the same time (details below) My first thought was that a new update of mongoose brought about this, but I'm not sure. Any help will be appreciated. Thanks

/.../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)

EDIT Looks like the error is thrown when I attempt to save. Here's the save method I call;

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);
    }
  });
}

and here's where i call it;

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

also, here's PreferenceModel.save method;

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

解决方案

I suggest you put _id related delete logic into your mongoose model Schema definition file :

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
};

Then in your callback execute toJSON :

var processedJson = resultDoc.toJSON();

to retrieve the processed version which nicely hides reusable logic.
NOTE : toJSON() is also magically executed by JSON.stringify()

这篇关于猫鼬错误:无法同时更新__v和__v的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆