简体   繁体   English

JSON中位置0处的意外令牌u无法用于异步

[英]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. 我对js和node.js相当陌生,但是我设法将调用转到了API并获取了必要的信息。 However when I was attempting to continue to raise the batter id to get the next information available. 但是,当我尝试继续提高击球员ID以获得下一个可用信息时。 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) 我现在尝试在每次运行后使整个函数异步延迟2秒,但是它返回以下错误(我假设是因为未定义某些内容)

**Note: When I just get the value for i=4 and p=1 the value does exist in the API data. **注意:当我刚得到i = 4且p = 1的值时,该值确实存在于API数据中。 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 . setTimeout不会将参数传递给它调用的函数,因此bodyundefined When you pass that to JSON.parse , it will be converted to the string "undefined" , which isn't a valid JSON text. 当您将其传递给JSON.parse ,它将被转换为字符串"undefined" ,这不是有效的JSON文本。

Nowhere is your code do you show any JSON coming into your program (or embedded into it). 您的代码无处显示任何传入(或嵌入到)程序中的JSON。 You need to have some JSON to parse before you try to parse it. 您需要先解析一些JSON,然后再尝试对其进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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