Wednesday 17 May 2017

Unexpected token u in JSON at position 0 not working for async

I am fairly new to js and node.js but I have managed to get the calls going to the API and getting the necessary information. However when I was attempting to continue to raise the batter id to get the next information available. I have successfully gotten the undefined error check to work as well. But I was unable to loop through because I was trying to perform something immediately on an async function. I am now trying to make the entire function async with a 2 second delay after each run, but it is returning the following error (i'm assuming because something is undefined)

**Note: When I just get the value for i=4 and p=1 the value does exist in the API data. However it gives this error when I attempt to start with those values using this code.

error:

Unexpected token u in JSON at position 0

this is my code:

   request('API Info redacted',
       setTimeout (function (err, response, body) {

        //do stuff below
        //to get the first play of the game, set i=1 and p=0
         var i = 4;
         var p = 1;
         // ************
         var boolIn = 1;

         // parse the body as JSON
         var parsedBody = JSON.parse(body);
         var apiResults = parsedBody.apiResults;

         if( typeof(apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p]) == 'undefined') {
            //sets the variables to the first batter of the next inning
            p=0;
            i = i+1; 
         }

         //below pulls the apiResults from the body of the API request
         var sportId = apiResults.sportId;
         var hitName = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].name;
         var fname = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].batter.firstName;
         var lname = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].batter.lastName;
         var outsB = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].outs.before;
         var outsA = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].outs.after;
         var rbis = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].runsBattedIn;
         var outDifference = (outsA - outsB);
         var hitB = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].baseSituation.beforeId;
         var hitA = apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p].baseSituation.afterId;
         var baseDifference = (hitA - hitB);


         //prints the details pulled above
         res.json("First Name: " + fname + ", Last Name: " + lname + ", Hit name: " + hitName + ", Outs on the play: " + outDifference + ", Rbi's: " + rbis +
         ", Base Sit: " + baseDifference);

         //increases the batter call
         p = p+1;

        //below ends the setTimeout
        }, 2000));

        //ended request
    });



via dgelinas21

No comments:

Post a Comment