I want to update form data in Node.js, but whenever i run this code it sets all the values to null

时间:2017-08-19 12:45:36

标签: node.js mongodb mongoose

app.put('/edit/:id', function(req, res) {
//new actor data
var actor = {
    'name': req.body.name,
    'dob': req.body.dob,
    'photo': req.file,
    'bio' : req.body.bio
};

 //updating actor
Actor.findOneAndUpdate({ _id:req.params.id }, { $set: actor }, { new: true }, function(err, data) {
    if (err) {
        res.send({
            status: false,
            error: err
        });
    } else {
        res.send({
            status: true,
            data: data
        });
    }

});
});

I have tried it with post also and taking id through body also but still it is not working. Tried it on postman as well as on frontend through form also. In postman also it is not able to take data or read data

1 个答案:

答案 0 :(得分:0)

试试这个以更新演员:

Actor.findOneAndUpdate({ _id:req.params.id }, {$set: {actor:actor},}, function(err, data) {
if (err) {
    res.send({
        status: false,
        error: err
    });
} else {
    res.send({
        status: true,
        data: data
    });
}

});