

用戶是我使用名稱、電子郵件和密碼創建的基本模式。但是當我嘗試更新它時,沒有發生錯誤,但 mongodb 中的集合沒有改變。在架構上嘗試了 {strict: false} 選項,但遺憾的是,它只是向其中添加了另一個物件,除錯模式也沒有多大幫助,所以現在我不知道如何解決這個問題。關于可能發生什么的任何建議?mongoose 版本是 6.0.12,mongodb 是 4.4.10,如果它們相關的話。
// Snippet for the code.
User.findOneAndUpdate({_id: req.body.id}, {updatedUser}, {useFindAndModify: false}, (err) => {
if(!err){
console.log("Managed to update")
res.json({message: "User updated successfully"})
} else {
res.json({message: `Something went wrong please try again => ${err} `})
}
})
uj5u.com熱心網友回復:
添加doc到回呼函式以查看您是否正在更新檔案,這樣您就可以獲得更好的錯誤訊息。
// Snippet for the code.
User.findOneAndUpdate({_id: req.body.id}, {updatedUser}, {useFindAndModify: false}, (err, doc) => {
if(!err){
console.log("Managed to update")
console.log(doc)
res.json({message: "User updated successfully"})
} else {
res.json({message: `Something went wrong please try again => ${err} `})
}
})
uj5u.com熱心網友回復:
_id通常和ObjectId,req.body.id通常是字串,所以它們不匹配。
findOneAndUpdate 如果找不到匹配的檔案,則成功。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360063.html
下一篇:換新護照前如何檢查密碼是否匹配
