Javascript 如何在mongoose中正确使用findOneAndUpdate?
我在这里读过所有类似的问题,但它们都不起作用。我正在尝试更新一个名为Transaction的模型,并使用findOneAndUpdate来更新它。没有错误,但文档只返回null。 代码: }) 请尝试以下方法:Javascript 如何在mongoose中正确使用findOneAndUpdate?,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,我在这里读过所有类似的问题,但它们都不起作用。我正在尝试更新一个名为Transaction的模型,并使用findOneAndUpdate来更新它。没有错误,但文档只返回null。 代码: }) 请尝试以下方法: app.post("/transaction/update", isAdmin, async (req, res) => { const username = "dennis" const count = req.body.count con
app.post("/transaction/update", isAdmin, async (req, res) => {
const username = "dennis"
const count = req.body.count
const date = req.body.date
const change = req.body.change
const service = req.body.service
const expires = req.body.expires
const transaction = await Transaction.findOneAndUpdate({ "username": "dennis" },
{
"count": count,
"date": date,
"change": change,
"service": service,
"expires": expires,
}, { new: true })
res.redirect("/admin")
})
您不必使用$set
或调用.save()
参考:
app.post("/transaction/update", isAdmin, async (req, res) => {
const username = "dennis"
const count = req.body.count
const date = req.body.date
const change = req.body.change
const service = req.body.service
const expires = req.body.expires
const transaction = await Transaction.findOneAndUpdate({ "username": "dennis" },
{
"count": count,
"date": date,
"change": change,
"service": service,
"expires": expires,
}, { new: true })
res.redirect("/admin")
})