Node JS 异步等待行为问题"/>
Node JS 异步等待行为问题
在如下所示的 Node.js 函数中 - 我在循环中为各种订单 ID 运行相同的函数。在
initialDocs2.push(orderIdString)
中,我看到了将要处理的所有订单 ID 的列表。然后有一个从数据库获取订单事务的异步操作。问题是在获取交易后,我将订单推送到另一个名为procsseddocs.push(orderIdString)
的数组中,在该数组中我得到了 orderId 的重复值(尽管 initialDocs2
中没有重复值)。为什么会这样?
async function flightBookingTransform(target_db, docOrder) {
try {
await target_db.collection("coltemp").insertOne({
_id: docOrder._id,
comment: "checking value"
})
// docTransform = await target_db.collection("template_data_orders_new").findOne({
// businessSubType: "FlightBooking"
// });
docTransform = flightOrderInitValue;
orderIdString = String(docOrder._id);
// procsseddocs.push(String(docOrder._id))
initialDocs2.push(orderIdString)
docTransactionarr = await target_db.collection("transaction").find({
"orderId.oid": orderIdString
}).sort({
"_id": -1
}).limit(1).toArray()
if (docTransactionarr && docTransactionarr.length != 0) {
docTransaction = docTransactionarr[0];
} else {
return;
}
txnarr.push(String(docTransaction._id))
if (!orderIdString) {
orderIdString = "null"
}
docTransform._id = new ObjectId(orderIdString);
procsseddocs.push(orderIdString)
docTransform.orderId = orderIdString;
回答如下:
更多推荐
Node JS 异步等待行为问题
发布评论