Skip to content Skip to sidebar Skip to footer

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. The connection is made properly and im also getting the attributes from the collection.

Solution 1:

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?

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

Solution 2:

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

Post a Comment for "Update By Id Not Working In Mongoose"