【发布时间】:2021-12-01 03:27:29
【问题描述】:
我将 mongoose 更新到最新版本 (6.0.2),现在我收到此错误并在执行 .updateOne() 时破坏应用程序。但是数据库内部的对象更新。我的代码:
async(req,res) => {
await Todo.updateOne(
{_id : req.params.id},
{
$set : {
status : "inactive"
}
},
(updateError) => {
// if updateError exist
if (updateError) {
// print error
printError(updateError);
// response the error
res.status(500).json({
error : "There was a Server Side Error!"
});
} else {
// if error dose not exist
// print message
console.log("|===> ????️ Data Was Updated successfully! ????️ <====|\n");
// response success
res.json({
message : "Todo Was Update successfully!"
});
}
});
}
【问题讨论】:
-
不要在异步/等待中使用回调
-
>>>> 在这种情况下为什么以及该怎么做???
-
因为不能同时使用,所以使用回调或者等待。如果您需要在 async/await 中捕获错误,请使用 try/catch 块
-
>>>> 谢谢。它现在工作。但还有一件事。当我插入数据时,我使用回调并等待,但这次它运行良好。只是它不起作用更新案例。为什么会发生?
标签: javascript node.js mongoose