mongoose .save()不是一个函数



面对错误.save()不是一个函数

我以前做过,但这次我不明白为什么它给我这个错误。虽然我可以从数据库查询数据,但它一直给我这个错误

代码:

router.post("/password-reset", (req,res) => {
signMeUp.findOneAndUpdate({email:req.body.resetmail})
.then(nuser => {
nuser.username,
nuser.email,
nuser.password = req.body.psd,
nuser.image 
})
signMeUp.save().then(()=>{
console.log("password changed  successfully !")
})
})

使用Model.findOneAndUpdate()函数的第二个参数传递您想要更新的属性:

router.post("/password-reset", (req,res) => {
signMeUp.findOneAndUpdate({ email: req.body.resetmail }, { password: req.body.psd })
.then(() => {
console.log("password changed  successfully !")
})
})

下面是Mongoose的await样式:

router.post("/password-reset", async (req, res) => { // add async so you can await inside
const nuser = await signMeUp.findOne({ email: req.body.resetmail }).exec()
// Do some stuff
await nuser.save();
console.log("password changed  successfully !")

res.end()
})

Or in one go withfindOneAndUpdate:

router.post("/password-reset", async (req, res) => { // add async so you can await inside
await signMeUp.findOneAndUpdate({ email: req.body.resetmail },
{ someField : "some update"})
.exec()
console.log("password changed  successfully !")

res.end()
})

你必须等待promise被解析因为现在它返回了一个promise


//Don't forget to make the function async
const nuser = await signMeUp.findOneAndUpdate({email:req.body.resetmail})


nuser.password = req.body.psd

await nuser.save();

或使用。then()

ignMeUp.findOneAndUpdate({email:req.body.resetmail})
.then(nuser => {
nuser.username,
nuser.email,
nuser.password = req.body.psd,
nuser.image 
nuser.save()
})

相关内容

  • 没有找到相关文章

最新更新