提问者:小点点

在axios中运行循环时抛出的错误


我有点新o打字稿和卡在axiosAPI电话。

有一种方法叫做“导入”,它用来获取发票号,并使其每5个发票号拼接。假设有8张发票。所以第一堆有5张发票,第二堆有3张发票。使用这种方法是因为API限制。它一次只允许5张。

async import() {   

//BatchOfFiles are somthing like this..... Api allows 5 invoices numbers at a time. 
//So There is code for making blocks per 5 invoices. 
//That code workd 100% fine and didn't add it because, issue is not there.
//batchOfFiles[0] ---> ["INV001","INV002","INV003","INV004","INV005"]
//batchOfFiles[1] ---> ["INV006","INV007","INV008"]

for (const batch of batchOfFiles) {
  let serchTextArr: SearchText[] = [];
  for (var idx in batch) {
    let searchText: SearchText = {
      Value: batch[idx],
      BusinessID: businessId
    };
    serchTextArr.push(searchText);
  }

    //CODES
    
  var data = JSON.stringify(msg);

  var config = {
    method: 'post',
    url: url,
    headers: headersRequest,
    data: data
  };
  //config is having proper url, credentials, headers and data. No issue in this.

  await axios(config)
    .then(async function (response) {
      let rootObj: RootNode = JSON.parse(JSON.stringify(response.data));
      //There are some codes and it also works fine
    })
    .catch(function (error) {
      console.log('---- Err ----')
    });

    }
 }

运行for循环时,API将被使用axios调用2次。第一次使用5个元素,第二次使用3个元素。我的问题是第一次循环运行没有任何错误。但是当它尝试使用循环运行第二次时,它会抛出一个错误。

这就是错误。

错误:在IncomingMessage. handleStreamEnd(/mnt/d/Freiteq/Tracking_Node/trace/node_modules/axios/lib/core/createError.js:16:15)在结算(/mnt/d/Freiteq/Tracking_Node/trace/node_modules/axios/lib/core/setle.js:17:12)在IncomingMessage.handleStreamEnd(/mnt/d/Freiteq/Tracking_Node/trace/node_modules/axios/lib/Adapters/http.js:237:11)在IncomingMessage.emit(events.js:327:22)在endReadableNT(_stream_readable.js:1220:12)在process TicksAndRejtions(内部/进程/task_queues.js:84:21)请求失败,状态码为500

任何人都可以帮助我找出问题所在…???


共1个答案

匿名用户

这是我的问题。当我向API发送数据时,它被请求一个参数的uuid,并且似乎它被API验证,无论发送的uuid是否重复。这就是我的代码中发生的事情。我在开始循环之前将uuid设置为变量。所以当它第一次运行时,它会成功运行。当第二次运行循环时,它具有相同的uuid并返回错误。我这么说是因为,其他开发人员可能会犯这种丑陋/轻微的错误并浪费他们宝贵的时间。