1

The Keys array contains 1000 project key's.

for (var i = 0; i < Keys.length; i++){
    httpRequest("https://hostname/api/issues/projectKeys="+Keys[i]+"?format=jSON",
        function (err, res, body) {
             var jsonObj = JSON.parse(body);
             issue.push(jsonObj.total);
          //   console.log(issue.length);

        }).auth(global.username, global.password);

}

The URL in the httpRequest returns a JSON. While I run this program, after 50-60 keys data is retrieved. My program is stopped due to an error.

SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()

Is it due to the asynchronous nature of NodeJS ? Please help How can I call the URL for 1000 times in a loop.

2 Answers2

2

Is it due to the asynchronous nature of NodeJS ?

Not directly. It's probably due to your application flooding whatever API you're hitting, and you're getting rate limited or some other error.

You should be looking at the response status code to decide if your response is okay. You should log these responses if there's an error. There's probably some text error message in the response.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I tried logging the error.
    Here is the response
    **{ Error: read ECONNRESET at _errnoException (util.js:1024:11) at TLSWrap.onread (net.js:615:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }**
    I'm not sure what it is :(
    – Mudabbir Pasha Apr 26 '18 at 18:59
  • 1
    @MudabbirPasha It means you're slamming that server so fast that it decided to just outright disconnect from you rather than bothering to reply. You can use an HTTP Agent and set the simultaneous requests down to something lower. Better yet though, figure out what the real limits are of the API and get a reasonable limit for your use case. – Brad Apr 26 '18 at 19:07
  • Thank you, I'll try using an HTTP agent and will reduce the keys count in my case. I'll try and come back again :) – Mudabbir Pasha Apr 26 '18 at 19:10
  • Can I use any Sleep() function between successive calls to the host. – Mudabbir Pasha Apr 27 '18 at 09:22
  • @MudabbirPasha You can do whatever you want. What you should be doing is properly handling errors. – Brad Apr 27 '18 at 14:30
0

Check this, how you can make API calls in node properly. Then always do the null/undefined or blank response check once you get the response back from the server before doing any processing.

  • I tried, but couldn't get any success can I use sleep or wait() between every call to the URL If yes, please let me know how can I use it . – Mudabbir Pasha Apr 27 '18 at 11:17
  • what kind of response you are getting from server and try to identify weather its error from server because if we make 1000 consecutive calls to a server then it may fails and start to send error response, so please check server response also once. – Shubham Tripathi May 06 '18 at 11:42