Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose error: Cannot update __v and __v at the same time

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);
  });
}
like image 817
user3863128 Avatar asked Jul 22 '14 04:07

user3863128


1 Answers

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()

like image 71
Scott Stensland Avatar answered Oct 14 '22 02:10

Scott Stensland