Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 猫鼬。按文档id更新引发[TypeError:无法读取未定义的_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 猫鼬。按文档id更新引发[TypeError:无法读取未定义的

Javascript 猫鼬。按文档id更新引发[TypeError:无法读取未定义的,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,这是我的代码: var fileModel = context.models.File, query = { _id: context.models.ObjectId("532083358ab1654c0c8b4ced") // TODO: for debug, change after update fix }, update = {

这是我的代码:

    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,

但是我不明白如何修复它。

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

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

这里没有读取任何对象的_id属性,所以我猜问题出在其他地方。您能告诉我们您在哪里/如何调用更新吗?仅此而已,fileModel.update==mongoose.Model.update。当我删除userId:context.data.userId时?context.models.ObjectId(context.data.userId):未定义,异常过期。但是我需要这个,我不能将
userId
设置为
undefined
。但是当我插入
文件时
我没有看到这个问题