0
votes

I'm bit new o typescript and stuck on axios API call.

There is a method called "import" and it use to get invoices numbers and make it splice per 5 invoice numbers. Lets think there are 8 invoices. So 1st pile has 5 invoices and 2nd pile has 3 invoices. That method used because the API limitation. It allows only 5 at a time.

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 ----')
    });

    }
 }

When running the for loop, API will be call with axios 2 times. 1st time with 5 elements and 2nd time with 3 elements. What my problem is 1st loop runs without any error. But when it try to run 2nd time with the loop, it is thrown an error.

This is the error.

Error: Request failed with status code 500 at createError (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/core/createError.js:16:15) at settle (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/mnt/d/Freiteq/Tracking_Node/tracking/node_modules/axios/lib/adapters/http.js:237:11) at IncomingMessage.emit (events.js:327:22) at endReadableNT (_stream_readable.js:1220:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)

Anyone can help me to find out where the issue is...????

1
"500" seems to be a response status code - have you tried console.loging the error in your catch or wrapping the whole await axios bit in a try / catch?Tyler Sebastian
Thanks for replying @TylerSebastian. Honestly didn't thrown error when I wrap axios by try catch. However I found my error. I'll post it as answers. I'm totally irresponsible... :(weeraa

1 Answers

0
votes

This is my issue. When I send data to API, it is requested uuid for one parameter and seems it validate by the API, whether sent uuid is duplicate or not. This is what happen in my code. I set uuid to variable before starting the loop. So when it runs in the first time, it will run successfully. When run the loop for 2nd time, it has same uuid and returns error. I said this because, other developers might do this kind of ugly/minor mistakes and ruin their valuable time.