gpt4 book ai didi

node.js - Mongoose save() 实际上并没有保存

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:57 25 4
gpt4 key购买 nike

我正在尝试查找我的记录、更新并保存它。我可以看到数据已更新,并且仍然可以在 save() 回调中显示更新后的数据。但是,当我访问数据库时,它实际上更新:

Skills.findOne({ skillsCat: req.body.skillsCat }, (err, gets)=> {
if (err) {
res.send(err)
return
}

if (gets && gets.skillName.indexOf(req.body.skillName) !== -1) {
// Here I update my data
gets.percent[gets.skillName.indexOf(req.body.skillName)] = req.body.percent
Console.log(gets); // Here I can see the data is updated

return gets.save((err, updated)=> {
Console.log(updated); // Here I can see the data is updated
if (err) { return }
res.json({
message: 'Skill updated successfully',
data: updated
})
})

} else {
return
}
})

有没有人遇到过类似的问题并帮助我解决这个问题?谢谢。

最佳答案

尝试下面的代码:

gets.percent[gets.skillName.indexOf(req.body.skillName)] = req.body.percent
Console.log(gets); // Here I can see the data is updated

//Notice that 'fieldname' is the name of actual field in database
gets.markModified('fieldname');

return gets.save((err, updated)=> {
Console.log(updated); // Here I can see the data is updated
if (err) { return }
res.json({
message: 'Skill updated successfully',
data: updated
})
})

关于node.js - Mongoose save() 实际上并没有保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com