Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While updating, mongoose is giving me an error: TypeError: Cannot set property 'vidName' of null

I am trying to update one particular field in the document. Here is my code:

module.exports.editAndUpdate = function(playerVideo, callback) {
    console.log(playerVideo.vidName);
    var newVidId = toString(playerVideo.vidId);
    myModel.findById(newVidId, function(err, doc) {
      if(err) throw err;
      doc.vidName = playerVideo.vidName;
      doc.save(callback);
    });
 }

Here playerVideo is a document, vidName and vidId are its fields.

Instead of doc.vidName = playerVideo.vidName, if I use doc.vidName = "Hey", then it is working perfectly fine. I used the typeof method and checked if playerVideo.vidName is a String and yes, it is.

Please help me solve this. Thanks for any suggestions :)

like image 804
psvs Avatar asked Jan 24 '26 18:01

psvs


1 Answers

For me, the issue occurred when I was updating a nested object, which had a fullstop in the key value.

e.g.

"$set": {
    "some.field": {
        "validuser": "this works",
        "invalid.user": "this breaks mongo"
    }
}

MongoDB doesn't support keys with a dot, and was providing this cryptic error:

cannot set property 'value' of null

Solution - don't store keys with a fullstop in it!

like image 147
Nick Grealy Avatar answered Jan 26 '26 10:01

Nick Grealy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!