node.js - findOneAndUpdate 更新数据库但挂起
问题描述
问题:我的异步函数确实更新了数据库,但没有继续执行脚本的其余部分。
//Process the data after you get it...
async function burnStatus(data) {
console.log("Burn Data Compiled Saving...");
let filter = { _id: "5e4454059a0be1c238b5f70b" };
if (data.error === true) {
console.log("Error retrieving burn data.");
var update = { update: burn.date, error: data.error };
} else {
var update = {
date: data.date,
precipitation: data.precipitation,
wind: { morning: data.wind.morning, afternoon: data.wind.afternoon },
ventIndex: data.ventIndex,
aqi: data.aqi,
updated: data.updated
};
}
console.log(update);
let doc = await BURN.findOneAndUpdate(filter, update, { upsert: true }, (error, result) => {
// *** NOTHING BELOW HERE EXECUTES EVEN THOUGH THE DB DOES GET UPDATED. ***
if (error) {
console.log(error);
} else {
console.log(result);
}
});
console.log("Burn Data Updated");
}
这是输出:
Getting Burn Data...
(node:16248) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Response Received Processing...
Precipitation: 0
Morning Wind: 6
Afternoon Wind: 8
Ventilation Index: POOR
AQI: 48
Burn Data Compiled Saving...
{
date: '02/13/20',
precipitation: 0,
wind: { morning: 6, afternoon: 8 },
ventIndex: 'POOR',
aqi: 48,
updated: '2020-02-13T12:42:11.201Z'
}
故障排除:
我已经设法追踪到它let doc = await BURN.findOneAndUpdate(filter, update, (error, result) => {
。我无法让它抛出一个错误让我捕捉(或者我没有尝试正确捕捉错误)。
所以我猜没有错误,我只是做错了一些事情。
解决方案
你在await
这里同时使用和回调(error, result)
你需要选择一个而不是两个。
等待
try {
let result = await BURN.findOneAndUpdate(filter, update, {upsert: true});
console.log("Burn Data Updated")
} catch (error) {
console.log(error)
}
打回来
let doc = BURN.findOneAndUpdate(
filter,
update,
{ upsert: true },
(error, result) => {
if (error) {
console.log(error);
} else {
console.log(result);
console.log("Burn Data Updated");
}
}
);
推荐阅读
- android - 在 sqlite 中查找列类型为 BLOB 的表
- android - Firebase 在 Unity3D 编辑器上运行良好,但在实际的 Android 设备上存在一些依赖问题
- c# - 在 ASP.NET Core 中注册 HostedService 的正确方法。AddHostedService 与 AddSingleton
- mysql - 如何检查表中是否存在变量?TCL
- swift - Swift嵌套枚举/结构以相反的顺序字符串
- c - 如何使用 sizeof 将数组的大小作为另一个参数传递
- sql - MS SQL Server - (当前日期 -1)
- ios - 以编程方式将子视图添加到自定义 UITableViewCell
- haskell - Parsec:解析满足某些谓词的字符串
- azure - 使用 U-SQL 消除一个特定列中的重复值和空值,同时保持第二列正确对齐