0

I know there are a lot of threads with this problem, but none of the resolutions presented there work for me, often because the problem is too specific. Before I explain my problem, here is my code:

User.findOne({authId: req.user.authId}, function(err, user){
        if(err) console.error(err);

        for(var i = 0; i<user.bot.messages.length; i++){
            if(user.bot.messages[i].name === req.body.commandName){
                console.log('found it');
                user.bot.messages[i].description = req.body.commandDescription;
            }
        }

        user.save(function(err){
            if(!err){
                console.log('saving user');
            }
        });
    });

What this code should do is update the description of a message after the user submits a form with said description. Usually when updating with mongoose I just use findOneAndUpdate() but that wouldn't work in this case, since it only accepts dot notation and I don't know beforehand which message is being updated.

Still, this should work as well, right? The multiple console.log() statements are just for debugging. The thing is though, I see all of those statements in the console when submitting the form, so I really don't know what the problem is in this case.
It just doesn't save, but I'm not getting any errors either.

I just found this in the mongoose docs. But how am I supposed to use markModified() if I don't know the exact path beforehand?

eRodY
  • 635
  • 1
  • 6
  • 10
  • Possible duplicate of [Mongoose: Find, modify, save](http://stackoverflow.com/questions/14199529/mongoose-find-modify-save) – Bertrand Martel Jul 19 '16 at 21:10
  • 1
    if you are using a mixed type like `{}` or `[]` you have to call `user.markModified('bot');` before saving to update that property. http://mongoosejs.com/docs/schematypes.html#mixed – aschmid00 Jul 19 '16 at 21:15
  • Thanks aschmid, that worked. – eRodY Jul 19 '16 at 21:22

0 Answers0