简体   繁体   中英

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

setTimeout will not pass arguments to the function it calls, so body is undefined . When you pass that to JSON.parse , it will be converted to the string "undefined" , which isn't a valid JSON text.

Nowhere is your code do you show any JSON coming into your program (or embedded into it). You need to have some JSON to parse before you try to parse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM