gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:23:14 25 4
gpt4 key购买 nike

我需要这方面的帮助。我有这个从一开始就一直在工作的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com