0
所以我想只更新傳遞的對象的值。mongoose findbyidandupdate剛通過值
Event.findByIdAndUpdate(req.body.id, {
$set: {
name: req.body.name,
narrative: req.body.narrative,
startdate: req.body.startdate,
enddate: req.body.enddate,
site: req.body.site
}
}, {new: true},
function(err, Event) {
if (err) throw err;
res.send(Event);
});
我現在的函數將null在post請求中沒有定義的任何字段。
例如,如果我的對象的所有字段定義,我嘗試更新只是名字:
{
"id": "57b8fa4752835d8c373ca42d",
"name": "test"
}
將導致:
{
"_id": "57b8fa4752835d8c373ca42d",
"name": "test",
"narrative": null,
"startdate": null,
"enddate": null,
"site": null,
"__v": 0,
"lastupdated": "2016-08-21T00:48:07.428Z",
"sponsors": [],
"attendees": []
}
有什麼辦法來執行具有了此更新通過所有其他領域呢?
你確定,你有你'req.body'所有的價值呢? –