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?