Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json node.js请求|有时出现意外的令牌错误,有时不会_Json_Node.js_Parsing_Syntax_Request - Fatal编程技术网

Json node.js请求|有时出现意外的令牌错误,有时不会

Json node.js请求|有时出现意外的令牌错误,有时不会,json,node.js,parsing,syntax,request,Json,Node.js,Parsing,Syntax,Request,我有一个for循环,它调用天气api 27次(27个不同的坐标) 返回的主体是字符串类型。 问题是,在执行JSON.parse时,有时会出现如下所示的错误,有时则不会。有时它做3个ok然后抛出错误,有时7个,有时只有1个,或者有时做27个ok然后不抛出错误。我看不出问题出在哪里,因为返回的27个数据对象总是相同的 SyntaxError: Unexpected token < at Object.parse (native) at Request._callback (/U

我有一个for循环,它调用天气api 27次(27个不同的坐标)

返回的主体是字符串类型。 问题是,在执行JSON.parse时,有时会出现如下所示的错误,有时则不会。有时它做3个ok然后抛出错误,有时7个,有时只有1个,或者有时做27个ok然后不抛出错误。我看不出问题出在哪里,因为返回的27个数据对象总是相同的

SyntaxError: Unexpected token <
    at Object.parse (native)
    at Request._callback (/Users/nofunat88/Documents/test/nodejs-express-ajax-test/borrar_final.js:111:29)
    at Request.self.callback (/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_modules/request/request.js:129:22)
    at Request.EventEmitter.emit (events.js:98:17)
    at Request.<anonymous> (/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_modules/request/request.js:873:14)
    at Request.EventEmitter.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_modules/request/request.js:824:12)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
SyntaxError:意外标记<
at Object.parse(本机)
应请求。_回调(/Users/nofunat88/Documents/test/nodejs-express-ajax-test/borrar_-final.js:111:29)
at Request.self.callback(/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_-modules/Request/Request.js:129:22)
at Request.EventEmitter.emit(events.js:98:17)
应要求。(/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_-modules/request/request.js:873:14)
at Request.EventEmitter.emit(events.js:117:20)
在收到消息时。(/Users/nofunat88/Documents/test/nodejs-express-ajax-test/node_-modules/request/request.js:824:12)
在IncomingMessage.EventEmitter.emit(events.js:117:20)
at_stream_readable.js:920:16
在进程中调用(node.js:415:13)

关于,

您应该检查请求过程中是否发生错误,然后-解析正文

request(url,function (err, resp, body){
                if (err) {
                    console.log(err);
                } else {
                    body = JSON.parse(body); 
                }

我真的不明白为什么,但在添加错误检查后,它不再显示任何语法错误!嘿,艾托尔,我面临着同样的错误,但进行错误检查并不能解决这个问题。我不知道它是如何断断续续的。你有其他的解决方案吗?对不起,我是这样解决的,也许你应该打开你自己的线程。。。
request(url,function (err, resp, body){
                if (err) {
                    console.log(err);
                } else {
                    body = JSON.parse(body); 
                }