首页 > 解决方案 > 在 axios 中运行循环时抛出错误

问题描述

我对打字稿有点陌生,并且坚持使用 axios API 调用。

有一种叫做“导入”的方法,它用于获取发票编号并使其每 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 个元素。我的问题是第一个循环运行没有任何错误。但是当它尝试使用循环第二次运行时,它会抛出一个错误。

这是错误。

错误:在结算 (/mnt/d/Friteq/Tracking_Node/ tracking/node_modules/axios/lib/core/settle.js:17:12) 在 IncomingMessage.handleStreamEnd (/mnt/d/Friteq/Tracking_Node/tracking/node_modules/axios/lib/adapters/http.js:237:11)在 IncomingMessage.emit (events.js:327:22) 在 endReadableNT (_stream_readable.js:1220:12) 在 processTicksAndRejections (internal/process/task_queues.js:84:21)

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

标签: typescriptaxios

解决方案


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


推荐阅读