Node JS 异步等待行为问题

编程入门 行业动态 更新时间:2024-10-06 22:24:25

<a href=https://www.elefans.com/category/jswz/34/1771450.html style=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 异步等待行为问题

本文发布于:2024-05-30 14:39:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Node   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!