Node.js 在for循环中使用wait

Node.js 在for循环中使用wait,node.js,mongodb,async-await,Node.js,Mongodb,Async Await,我有一个功能: 从mongodb请求数据 根据结果向Ebay发出API调用 将易趣API响应插入另一个mongodb 我有一个版本(甚至更黑客)正在工作(见),但并不是所有的调用都被执行 我曾尝试将函数转换为async/await,但遇到错误:套接字挂起错误。根据我调用Ebay API的经验,这并不是因为我向API端点(即我获取的URL)发出了请求,而是函数其余部分出现了一些错误 我想知道这是否与我在for/of循环中的wait有关 是否有人可以建议使用async/await构造此函数的正确方法

我有一个功能:

  • 从mongodb请求数据
  • 根据结果向Ebay发出API调用
  • 将易趣API响应插入另一个mongodb
  • 我有一个版本(甚至更黑客)正在工作(见),但并不是所有的调用都被执行

    我曾尝试将函数转换为async/await,但遇到
    错误:套接字挂起
    错误。根据我调用Ebay API的经验,这并不是因为我向API端点(即我获取的URL)发出了请求,而是函数其余部分出现了一些错误

    我想知道这是否与我在
    for/of
    循环中的
    wait
    有关

    是否有人可以建议使用async/await构造此函数的正确方法

    exports = async function(payload) {
    const axios = require("axios");
    const throttledQueue = require('throttled-queue'); // not used in this example, but I need to throttle the requests once I figure out the rest of the function
    let throttle = throttledQueue(5, 1000, true);
    
    let ebayCollection = context.services.get("mongodb-atlas").db("cards").collection("ebay-prices");
    let cardCollection = context.services.get("mongodb-atlas").db("cards").collection("stuart collection");
    
    
    //Get wanted cards from card collection
    let ownedCards = await cardCollection.find({
        forTrade: "Yes"
    }).toArray(); //change back to status:Want after testing.
    
    
    async function insertCards(ownedCards) {
        try {
            for (let card of ownedCards) {
                let url = "http://svcs.ebay.com/services/search/FindingService/v1?GLOBAL-ID=EBAY-US&REST-PAYLOAD&keywords=" + card.brand + " " + card.series + " " + card.player +
                    "&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&OPERATION-NAME=findCompletedItems&paginationInput.entriesPerPage=6&paginationInput.pageNumber=1&RESPONSE-DATA-FORMAT=json&SECURITY-APPNAME=<myapp>&SERVICE-NAME=FindingService&SERVICE-VERSION=1.12.0";
                let cardId = card.card_id;
                console.log(url);
                const resp = await axios.get(url);
                console.log(resp);
                for (const item in resp.data.findCompletedItemsResponse[0].searchResult[0].item) {
                    const itemId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].itemId[0];
                    const title = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].title[0];
                    const galleryURL = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].galleryURL[0];
                    const endDate = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].listingInfo[0].endTime[0];
                    const price = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0].__value__;
                    const currencyId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0]['@currencyId'];
    
                    const result = ebayCollection.updateOne({
                        itemId: itemId
                    }, {
                        $addToSet: {
                            cardId: cardId
                        },
                        $set: {
                            itemId: itemId,
                            title: title,
                            endDate: endDate,
                            price: price,
                            currencyId: currencyId,
                            galleryURL: galleryURL
                        }
                    }, {
                        upsert: true
                    })
    
                }
            }
    
        } catch (error) {
            console.log("THIS IS THE ERROR" + error); // catches both errors
        }
    }
    
    await insertCards(ownedCards);
    };
    
    exports=异步函数(有效负载){
    常量axios=要求(“axios”);
    const throttledQueue=require('throttled-queue');//在本例中未使用,但我需要在了解函数的其余部分后对请求进行限制
    让throttle=throttledQueue(51000,true);
    让ebayCollection=context.services.get(“mongodb atlas”).db(“cards”).collection(“ebay价格”);
    让cardCollection=context.services.get(“mongodb atlas”).db(“cards”).collection(“stuart collection”);
    //从卡片收集处获取通缉卡
    let ownedCards=等待cardCollection.find({
    forTrade:“是的”
    }).toArray();//更改回状态:测试后需要。
    异步函数插入卡(自有卡){
    试一试{
    用于(出租自有卡中的卡){
    让url=”http://svcs.ebay.com/services/search/FindingService/v1?GLOBAL-ID=EBAY-US&REST-PAYLOAD&keywords=“+card.brand+”“+card.series+”“+card.player”+
    “&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&OPERATION-name=findCompletedItems&paginationInput.entriesPerPage=6&paginationInput.pageNumber=1&RESPONSE-DATA-FORMAT=json&SECURITY-APPNAME=&SERVICE-name=FindingService&SERVICE-VERSION=1.12.0”;
    设cardd=card.card\u id;
    console.log(url);
    const resp=等待axios.get(url);
    控制台日志(resp);
    对于(resp.data.findCompletedItemsResponse[0].searchResult[0].item中的常量项){
    const itemId=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。项ID[0];
    常量title=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。标题[0];
    const galleryURL=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。galleryURL[0];
    const endDate=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。listingInfo[0]。endTime[0];
    const price=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。sellingStatus[0]。currentPrice[0]。\u值\u;
    const currencyId=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。sellingStatus[0]。currentPrice[0]['@currencyId'];
    const result=ebayCollection.updateOne({
    itemId:itemId
    }, {
    $addToSet:{
    卡迪德:卡迪德
    },
    $set:{
    itemId:itemId,
    标题:标题,,
    endDate:endDate,
    价格:价格,
    currencyId:currencyId,
    galleryURL:galleryURL
    }
    }, {
    厄普塞特:是的
    })
    }
    }
    }捕获(错误){
    console.log(“这是错误”+错误);//捕获两个错误
    }
    }
    等待插入卡(自有卡);
    };
    
    好的,在查看了关于这个问题的所有评论之后,我将请求更改为https,问题已经停止

    我对我的代码做了一些调整,并加入了
    节流队列
    ,以启用速率限制-如果对任何人都有用,请参阅下面的内容

    exports = async function(payload) {
    const axios = require("axios");
    const throttledQueue = require('throttled-queue');
    let throttle = throttledQueue(15, 10000, true);
    
    let ebayCollection = context.services.get("mongodb-atlas").db("cards").collection("ebay-prices");
    let cardCollection = context.services.get("mongodb-atlas").db("cards").collection("stuart collection");
    
    
    //Get wanted cards from card collection
    let ownedCards = await cardCollection.find({status: "Want"}).toArray(); //change back to status:Want after testing.
    
    
    async function insertCards(ownedCards) {
        try {
          for (let card of ownedCards) {
            let url = "https://svcs.ebay.com/services/search/FindingService/v1?GLOBAL-ID=EBAY-US&REST-PAYLOAD&keywords=" + card.brand + " " + card.series + " " + card.player +
                "&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&OPERATION-NAME=findCompletedItems&paginationInput.entriesPerPage=6&paginationInput.pageNumber=1&RESPONSE-DATA-FORMAT=json&SECURITY-APPNAME=<myapp>&SERVICE-NAME=FindingService&SERVICE-VERSION=1.12.0";
                let cardId = card.card_id;
                console.log(url);
               const resp = await axios.get(url);
               console.log(resp);
               for (const item in resp.data.findCompletedItemsResponse[0].searchResult[0].item) {
                const itemId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].itemId[0];
                const title = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].title[0];
                const galleryURL = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].galleryURL[0];
                const endDate = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].listingInfo[0].endTime[0];
                const price = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0].__value__;
                const currencyId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0]['@currencyId'];
    
                const result = ebayCollection.updateOne({
                    itemId: itemId
                }, {
                    $addToSet: {
                        cardId: cardId
                    },
                    $set: {
                        itemId: itemId,
                        title: title,
                        endDate: endDate,
                        price: price,
                        currencyId: currencyId,
                        galleryURL: galleryURL
                    }
                }, {
                    upsert: true
                })
    
               }
          }
    
        } catch (error) {
          console.log("THIS IS THE ERROR"+ error); // catches both errors
        }
      }
    
      throttle(
        await function() {
            // make the request.
            insertCards(ownedCards);
        });
    };
    
    exports=异步函数(有效负载){
    常量axios=要求(“axios”);
    const throttledQueue=require('throttled-queue');
    让throttle=throttledQueue(15,10000,true);
    让ebayCollection=context.services.get(“mongodb atlas”).db(“cards”).collection(“ebay价格”);
    让cardCollection=context.services.get(“mongodb atlas”).db(“cards”).collection(“stuart collection”);
    //从卡片收集处获取通缉卡
    let ownedCards=wait cardCollection.find({status:“Want”}).toArray();//测试后更改回status:Want。
    异步函数插入卡(自有卡){
    试一试{
    用于(出租自有卡中的卡){
    让url=”https://svcs.ebay.com/services/search/FindingService/v1?GLOBAL-ID=EBAY-US&REST-PAYLOAD&keywords=“+card.brand+”“+card.series+”“+card.player”+
    “&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&OPERATION-name=findCompletedItems&paginationInput.entriesPerPage=6&paginationInput.pageNumber=1&RESPONSE-DATA-FORMAT=json&SECURITY-APPNAME=&SERVICE-name=FindingService&SERVICE-VERSION=1.12.0”;
    设cardd=card.card\u id;
    console.log(url);
    const resp=等待axios.get(url);
    控制台日志(resp);
    对于(resp.data.findCompletedItemsResponse[0].searchResult[0].item中的常量项){
    const itemId=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。项ID[0];
    常量title=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。标题[0];
    const galleryURL=resp.data.findCompletedItemsResponse[0]。搜索结果[0]。项[item]。galleryURL[0];
    const endDate=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。listingInfo[0]。endTime[0];
    const price=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。sellingStatus[0]。currentPrice[0]。\u值\u;
    const currencyId=resp.data.findCompletedItemsResponse[0]。searchResult[0]。item[item]。sellingStatus[0]。curren