当嵌套在异步函数和回调中时,不会调用回调

问题描述

我有一段代码,该代码使用readline迭代文件,使用fast-csv解析该行,然后发出Web请求。在这种情况下,不会调用处理Web响应的代码。如果我将Web请求和响应处理代码从readline循环中移出,并删除处理Web请求/响应的CSV,就可以了。我在做什么错了?

注意:该代码最终将处理一个具有100,000条记录的文件,因此,将文件加载或将csv记录解析到内存中不是一个选择。我对打字稿比较陌生,但是有其他语言的经验,我假设我在犯一些简单的错误

出于各种隐私原因,我将代码简化为仅几个页面,并将Web请求定向到pipedream服务器。

数据

id,名称,地址,国家、、、、、、、、、 122959,丹东SLT服装实业有限公司,“中国辽宁省丹东市千阳经济开发区振阳路32号”,中国,,,,,, 673,惠州市惠益针织有限公司,“中国惠州市”,中国,122555,J。亚当斯有限公司(Adams&Associates Limited),“延安区郭健路1-31号7楼”,“台湾地区” ,,,,,,,,

这是示例代码

@H_404_15@const fs = require('fs');
const readline = require('readline');
const csv = require("fast-csv");
const fetch2 = require("node-fetch");

let mainUrl = "https://57892cf21d2e754a7342599fe3b6a3b5.m.pipedream.net";
let apiKey = "00000000";

// records written to output file
let recordsProcessed = 0;


async function getData (id : string,name : string,address : string,country : string) : Promise<any>
{
    // create request data to query the API
    // NOTE: the authorization header is the work "Token" followed by the API
    // key for an authorized used of the API.

    // add the query string specifying public false and create false to prevent
    // any creation of public address data
    let url : string = `${mainUrl}?create=false&public=false`;
    let authToken = `Token ${apiKey}`;

    let request = {
        method: 'POST',headers: {
            "Accept": 'application/json',"Content-Type": 'application/json',"Authorization": `${authToken}`
        },body: `{ "country": "${country}","name": "${name}","address": "${address}"  }`
    };

    let result;

    const resp = await fetch2(url,request);
    if ((resp) && ((resp.status == 200) || (resp.status == 201)))
    {
        console.log ("ok");

        let parsedBody = await resp.json();
        result = { body: `${parsedBody}` };
    }
    else
    {
        console.log (`Error request response status = ${resp.status} - ${id},"${name}","${address}","${country}"`);
    }

    return (result);
}



/**
 * Main processing loop
 */
(async () =>
{
    let errCode : number = 0;

    try
    {
        let skipFirstLine = true;
        let inFile = "../test.csv";
        let count : number = 0;

        // create a file stream that will be read a line at a time,// each like will be CSV parsed and made into a web
        // request.  The response from the web request will
        // be captured and logged.
        const fileStream = fs.createReadStream (inFile);
        const rl = readline.createInterface({
            input: fileStream,crlfDelay: Infinity
        });

        for await (let line of rl)
        {
            count++;

            // skip first line
            if ((skipFirstLine) && (count != 1) && (line.length))
            {
                line = line.trim ();

                console.log (`processing line ${count}...`)

                // parsing options,basic
                const options = {
                    delimiter: ",",trim: "true",comment: "true"
                }

                let dt;

                await csv.fromString (line,options)
                    .on("data",async (data : any) =>
                    {
                        let id: string = data[0];
                        let name: string = data[1];
                        let address: string = data[2];
                        let country: string = data[3];

                        dt = await getData (id,name,address,country);
                        if (dt)
                        {
                            console.log (`record ok`)
                            recordsProcessed++;
                        }

                    })
                    .on ('error',(err : Error) =>
                    {
                        console.log (`error ${err}`);
                    });
            }
        }

        // this works just fine
        console.log ('testing data...')
        let name = "Gelal Socks Company Şabanözü";
        let address = "Cumhuriyet,Orta Yolu,18650 Şabanözü/Çankırı,Turkey";
        let country = "Turkey";

        let d = await getData ("12345678",country);
        if (d)
        {
            console.log (`test address OK.`)
        }

    }
    catch (e)
    {
        console.log (`Unable to run analysis.`);
    }

    process.exit (errCode);
})();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)