Mongoose findOneAndUpdate Updating Multiple Fields

前端 未结 1 1870
再見小時候
再見小時候 2021-02-01 05:55

The findOneAndUpdate method doesn\'t work properly. I\'m trying to update all the fields all at once but it\'s only updating (setting) the last field. It\'s always only the last

相关标签:
1条回答
  • 2021-02-01 06:43

    You are using the $set operator multiple times. The correct syntax for $set is :

    { $set: { <field1>: <value1>, ... } }
    

    You need to change your update argument like this:

    Book.findOneAndUpdate({ "_id": bookId }, { "$set": { "name": name, "genre": genre, "author": author, "similar": similar}}).exec(function(err, book){
       if(err) {
           console.log(err);
           res.status(500).send(err);
       } else {
                res.status(200).send(book);
       }
    });
    
    0 讨论(0)
提交回复
热议问题