我正在嘗試更新用戶并使用節點 js 在 mongo Db 中獲取更新的資料。資料庫資料正在更新,但我收到過時的資料作為回應。
這是我的更新功能
exports.update = (req, res) => {
if (!req.body) {
res.status(400).send({ message: "Content cannot be empty" })
return
}
const id = req.params.id
studentDB.findByIdAndUpdate(id, req.body)
.then(data => {
if (!data) {
res.status(404).send({ message: `Cannot update user with ${id}. Maybe user not found!` })
} else {
res.send(data)
}
})
.catch(err => {
res.status(500).send({ message: err.message || "Some error occured while creating a update method" })
})
}
這是資料庫用戶資料
{
"_id": "627985c905dcb255365a2c0e",
"fullName": "Thambili Kankanamlage Ruwan Rohitha",
"nic": "2021345475655",
"address": "Old air port road, Nagoda, Kalutara",
"telephone": "0761234567",
"email": "[email protected]",
"__v": 0
}
當我更新任何欄位(例如“nic”為“5433243532324”)時,盡管資料庫已更新,但我還是將以前的資料作為回應
像這個“nic”:“2021345475655”
但我想要這樣的“nic”:“5433243532324”
uj5u.com熱心網友回復:
您必須通過帶有新屬性的選項物件來獲取更新的資料
studentDB.findByIdAndUpdate(id, req.body,{new:true})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472217.html
