admin管理员组文章数量:1406178
Trying to update data using PUT request. But, the data is not updating and returning the previous data in postman.
Postman put request:
http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom
Postman response:
{
"createdAt": "2019-10-19T04:16:13.317Z",
"updatedAt": "2019-10-19T04:16:13.317Z",
"_id": "5daa8f1c5845ad0b5826b8d9",
"name": "scarlett johansson",
"birthday": "1980-10-14T00:00:00.000Z",
"country": "usa",
"__v": 0
}
I have also tried to use findByIdAndUpdate. Didn't get the result. Any help would be appreciated.
Controller:
exports.updateActor = async(req, res, next) => {
try {
const actorId = req.params.actorId;
const data = req.body;
const updateActor = await Actor.findById(actorId);
updateActor.set(data);
const actor = await updateActor.save();
// res.status(200).json({ message: "Data has Updated Successfully!" });
res.send(actor);
} catch (err) {
res.status(500).json({ message: err.message });
}
};
Router:
router.put('/actors/:actorId', Actor.updateActor);
Trying to update data using PUT request. But, the data is not updating and returning the previous data in postman.
Postman put request:
http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom
Postman response:
{
"createdAt": "2019-10-19T04:16:13.317Z",
"updatedAt": "2019-10-19T04:16:13.317Z",
"_id": "5daa8f1c5845ad0b5826b8d9",
"name": "scarlett johansson",
"birthday": "1980-10-14T00:00:00.000Z",
"country": "usa",
"__v": 0
}
I have also tried to use findByIdAndUpdate. Didn't get the result. Any help would be appreciated.
Controller:
exports.updateActor = async(req, res, next) => {
try {
const actorId = req.params.actorId;
const data = req.body;
const updateActor = await Actor.findById(actorId);
updateActor.set(data);
const actor = await updateActor.save();
// res.status(200).json({ message: "Data has Updated Successfully!" });
res.send(actor);
} catch (err) {
res.status(500).json({ message: err.message });
}
};
Router:
router.put('/actors/:actorId', Actor.updateActor);
Share
Improve this question
asked Oct 19, 2019 at 4:49
tanjiyatanjiya
1813 silver badges18 bronze badges
4 Answers
Reset to default 3To resolve the ObjectId error use the below code.
var mongoose = require('mongoose');
const updateActor = await Actor.findOneAndUpdate({"_id":mongoose.Types.ObjectId(actorId)},data, { new: true });
res.send(updateActor);
Your postman request is http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom
so look like the data to update will be in req.query
instead of req.body
.
Note: You should put the data to update in the body instead of query like you're doing.
More info here.
Please use following code for getting update data
Actor.findOneAndUpdate({"_id":ObjectId(actorId)},data,
{ new: true}).then((updatedData) => {
res.send(updatedData);
});
exports.updateActor = async(req, res, next) => {
try {
const actorId = req.params.actorId;
const data = req.body;
const updateActor = await Actor.update({"_id":actorId},data);
// res.status(200).json({ message: "Data has Updated Successfully!" });
res.send(updateActor);
} catch (err) {
res.status(500).json({ message: err.message });
}
};
try thiss...
本文标签: javascriptNode rest API put request is not updating the request dataStack Overflow
版权声明:本文标题:javascript - Node rest API: put request is not updating the request data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744349696a2601973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论