简体   繁体   English

通过 id 更新在猫鼬中不起作用

[英]update by id not working in mongoose

I have a code in which i am making a new connection using mongoose and using a schema also.我有一个代码,我在其中使用 mongoose 和模式建立新连接。 The connection is made properly and im also getting the attributes from the collection.连接正确,我也从集合中获取属性。 After that i want to update in my collection, and that is not working.. I have following code之后我想在我的收藏中更新,但那不起作用..我有以下代码

console.log("id : "+content._id); 
Alert.findByIdAndUpdate(content._id, {
    $set: {
        status: true
    }
}, function(error, result) {
    console.log(result);
    if (error) console.log(error);
    else {
        console.log('Alert updated');
    }
});

It shows alert updated but status remains false..它显示警报已更新,但状态仍然是错误的..

result for 1st console is 
id : 55096a5f91169c8e1673af20; 

and 2nd console is
Alert updated

Thanks and regards in advance提前致谢和问候

Your code looks perfect, the id looks like it's correct and your code don't return any errors, so have you checked your schema to see if it contains the "status" field?您的代码看起来很完美,id 看起来是正确的,并且您的代码没有返回任何错误,所以您是否检查了架构以查看它是否包含“状态”字段?

Looks like you can create and edit new documents with a schema that is not equal on both sides, application an data layers.看起来您可以创建和编辑具有两侧不相等的架构的新文档,应用程序和数据层。

More info here: https://github.com/Automattic/mongoose/issues/1060更多信息在这里: https : //github.com/Automattic/mongoose/issues/1060

Your code also looks right to me, maybe try one of the following?您的代码对我来说也很合适,也许可以尝试以下方法之一?

Alert.findOne({_id : content._id}, function(error, alert) {
    if (error) console.log(error);
    alert.status = true;
    alert.save();
});

Or或者

 Alert.findOneAndUpdate({_id : content._id}, {
        $set: {
            status: true
        }
    }, function(error, result) {
        console.log(result);
        if (error) console.log(error);
        else {
            console.log('Alert updated');
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM