【问题标题】:Mongoose. Update by document id throws [TypeError: Cannot read property '_id' of undefined]猫鼬。按文档 id 更新抛出 [TypeError: Cannot read property '_id' of undefined]
【发布时间】: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


【解决方案1】:

我通过单独的部分代码来解决这个问题。但我不明白我的第一个解决方案出了什么问题。这是工作代码:

var fileModel = context.models.File,
        query = {
            _id: {
                $in: context.data.files.map(function (el) {
                    return context.models.ObjectId(el);
                })
            }
        },
        update = {
            description: context.data.description,
            isAdded: true
        };
    if (context.data.userId){
        update.userId = context.models.ObjectId(context.data.userId);
    }
    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 });
        }
    });

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 2020-11-07
    • 2013-04-22
    • 2021-06-25
    • 2015-12-02
    • 2017-04-24
    • 2023-03-15
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多