node.js Mongoose - findByIdAndUpdate - 不适用于 req.body

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27108177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:09:20  来源:igfitidea点击:

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

node.jsmongodbexpressmongoose

提问by Robert

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

我在通过 mongoose 更新 mongodb 中的文档时遇到问题。

My model bellow:

我的模型如下:

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

var UserSchema = new mongoose.Schema({
    first_name:{
        type: String
    },
    last_name:{
        type: String
    },
    email:{
        type: String,
        unique: true,
        required: true
    },
    password:{
        type: String,
        required: true
    },
    is_active:{
        type: Boolean,
        default: true
    },
    last_login:{
        type: Date
    }
});
module.exports = mongoose.model('User', UserSchema);

Controller put function bellow:

控制器放置功能如下:

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

Output on console:

控制台输出:

Listening on port 3000... { first_name: 'Michal', last_name: 'Test' } 
PUT /api/users/54724d0fccf520000073b9e3 200 58.280 ms - 4

The printed params are provided as form-data (key-value). Looks that is not working at least for me any idea what is wrong here?

打印的参数作为表单数据(键值)提供。看起来至少对我不起作用,知道这里有什么问题吗?

回答by Barno

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

你必须req.params.user_id改用req.body.user_id

exports.updateUser = function (req, res) {   
    console.log(req.body);

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

回答by Robert

I found the mistake. Note that I'm calling

我发现了错误。请注意,我正在打电话

req.body.user_id

req.body.user_id

where should be

应该在哪里

req.params.user_id

req.params.user_id

  • url is (PUT) http://127.0.0.1:3000/api/users/54724d0fccf520000073b9e3
  • 网址是 (PUT) http://127.0.0.1:3000/api/users/54724d0fccf520000073b9e3

回答by Neelavar

Further, the req.bodywould have key value as Text and realized as Stringobject, inside the code. Thus, it is useful to parse the string into JSON using JSON.parse(req.body.user)- while the useris the key and { first_name: 'Michal', last_name: 'Test' }is the value.

此外,req.body将键值作为文本并String在代码内部实现为对象。因此,使用JSON.parse(req.body.user)-将字符串解析为 JSON 很有用,而user是键和{ first_name: 'Michal', last_name: 'Test' }值。

console.log(req.body);
var update = JSON.parse(req.body.user);
var id = req.params.user_id;
User.findByIdAndUpdate(id, update, function(err, result){
    if(err){
        console.log(err);
    }
    console.log("RESULT: " + result);
    res.send('Done')
});

Note: the updatevalue is sent to Mongo DB as

注意:更新值发送到 Mongo DB 作为

{$set: { first_name : 'Michal`, last_name: 'Test' } 

Further reference: Mongoose JS documentation - findByIdAndUpdate

进一步参考:Mongoose JS 文档 - findByIdAndUpdate