Mongoose - findByIdAndUpdate - doesn't work with req.body

后端 未结 3 1988
孤街浪徒
孤街浪徒 2021-02-14 17:57

I have a problem with update documents in mongodb over mongoose.

My model bellow:

var mongoose = require(\'mongoose\');
var bcrypt = require(\'bcrypt-nod         


        
3条回答
  •  梦如初夏
    2021-02-14 18:35

    You have to use req.params.user_id instead req.body.user_id

    exports.updateUser = function (req, res) {   
        console.log(req.body);
    
        User.findByIdAndUpdate(req.params.user_id,{$set:req.body},{new:true}, function(err, result){
            if(err){
                console.log(err);
            }
            console.log("RESULT: " + result);
            res.send('Done')
        });
    };
    

提交回复
热议问题