【发布时间】:2014-03-17 15:43:54
【问题描述】:
这是我的代码:
var fileModel = context.models.File,
query = {
_id: context.models.ObjectId("532083358ab1654c0c8b4ced") // TODO: for debug, change after update fix
},
update = {
description: context.data.description,
userId: context.data.userId ?
context.models.ObjectId(context.data.userId) : undefined,
isAdded: true
};
fileModel.update(query, update, { multi: true }, function (err) {
if (err) {
console.log('update');
console.log(err);
context.sendJson({ success: false, err: err });
}
else {
context.sendJson({ success: true });
}
});
这是我的架构:
var fileSchema = new schema({
path: { type: String, required: true, validate: [validateName, 'a path is required'] },
isApproved: { type: Boolean, default: false },
isAdded: { type: Boolean, default: false },
name: { type: String, required: true, validate: [validateName, 'a name is required'] },
description: { type: String },
userId: { type: schema.Types.ObjectId },
updated: { type: Date, default: Date.now },
size: { type: Number }
}, { autoIndex: false });
当我尝试按 id 更新文档时,我在控制台中看到以下消息:
update
[TypeError: Cannot read property '_id' of undefined]
我觉得有问题
userId: context.data.userId ?
context.models.ObjectId(context.data.userId) : undefined,
但我不明白如何解决它。
【问题讨论】:
-
这里没有读取任何对象的 _id 属性,所以我猜问题出在其他地方。你能告诉我们你在哪里/如何调用更新吗?
-
就是这样,fileModel.update == mongoose.Model.update。当我删除 userId: context.data.userId ? context.models.ObjectId(context.data.userId) :未定义,异常过期。但我需要这个
-
我无法将
userId设置为undefined。但是当我插入file时,我没有看到这个问题
标签: javascript node.js mongodb mongoose