Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
Node.js SyntaxError:Object.parse(本机)npm请求时输入意外结束_Node.js_Request - Fatal编程技术网

Node.js SyntaxError:Object.parse(本机)npm请求时输入意外结束

Node.js SyntaxError:Object.parse(本机)npm请求时输入意外结束,node.js,request,Node.js,Request,你好,我不明白我为什么会有这个错误,我以为回调是在收到数据后执行的,知道这是从哪里来的吗? 非常感谢 节点错误: SyntaxError: Unexpected end of input at Object.parse (native) 我解析主体的答案,将其发送到计算函数,然后再发送到页面=/ var options = { method: 'POST', url: self.rippledataapiProxyHost.account_offers_ex

你好,我不明白我为什么会有这个错误,我以为回调是在收到数据后执行的,知道这是从哪里来的吗? 非常感谢

节点错误:

SyntaxError: Unexpected end of input
  at Object.parse (native)
我解析主体的答案,将其发送到计算函数,然后再发送到页面=/

var options = {
        method: 'POST',
        url: self.rippledataapiProxyHost.account_offers_exercised,
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        body:parameters 
    };

    var callback = function(error, response, body) {
        if (error) {
            console.log('error', error);
            res.send(500, 'something went wrong');
        }
        console.dir("bodyyyyyyyy====>",body);
        var rippleoffersexercised = new self.datacalcul.rippleoffersexercised;
        var data = JSON.parse(body);
        var datas = rippleoffersexercised.calculate(data);
        res.status(response.statusCode).send(datas);
    }
    request(options, callback);
以下是堆栈跟踪:

'bodyyyyyyyy====>'

SyntaxError: Unexpected end of input
  at Object.parse (native)
  at Request.callback [as _callback] (/home/francois/dev/ripplereport/webserver-newclientFrancois/server/middlewares/proxy/rippledataapiProxy.js:77:20)
  at Request.self.callback (/home/francois/dev/ripplereport/webserver-newclientFrancois/node_modules/request/request.js:344:22)
  at Request.emit (events.js:98:17)
  at Request.<anonymous> (/home/francois/dev/ripplereport/webserver-newclientFrancois/node_modules/request/request.js:1239:14)
  at Request.emit (events.js:117:20)
  at IncomingMessage.<anonymous> (/home/francois/dev/ripplereport/webserver-newclientFrancois/node_modules/request/request.js:1187:12)
  at IncomingMessage.emit (events.js:117:20)
  at _stream_readable.js:943:16
  at process._tickCallback (node.js:419:13)

[gulp] [nodemon] app crashed - waiting for file changes before starting...
'bodyyyyyyyyy====>'
SyntaxError:输入意外结束
at Object.parse(本机)
atrequest.callback[as_callback](/home/francois/dev/ripplereport/webserver newclientFrancois/server/middleware/proxy/rippledataapiProxy.js:77:20)
at Request.self.callback(/home/francois/dev/ripplereport/webserver newclientFrancois/node_modules/Request/Request.js:344:22)
at Request.emit(events.js:98:17)
应要求。(/home/francois/dev/ripplereport/webserver newclientFrancois/node_modules/request/request.js:1239:14)
at Request.emit(events.js:117:20)
在收到消息时。(/home/francois/dev/ripplereport/webserver newclientFrancois/node_modules/request/request.js:1187:12)
在IncomingMessage.emit(events.js:117:20)
在_stream_readable.js:943:16
在进程中调用(node.js:419:13)
[gulp][nodemon]应用程序崩溃-在启动之前等待文件更改。。。

如评论中所述,您可能会收到空的或格式错误的请求,从而导致
JSON.parse
抛出。类似的内容应该可以帮助您:

var callback = function(error, response, body) {
    if (error) {
        console.log('error', error);
        return res.send(500, 'something went wrong');
    }
    try {
        var data = JSON.parse(body);
    } catch(e) {
        console.log('malformed request', body);
        return res.status(400).send('malformed request: ' + body);
    }
    console.log('body', body);
    var rippleoffersexercised = new self.datacalcul.rippleoffersexercised;
    var datas = rippleoffersexercised.calculate(data);
    return res.status(response.statusCode).send(datas);
}

console.dir(body)
显示了什么?它显示了一个对象,这很奇怪,它正在工作,但有时它崩溃了,无法解释为什么
console.dir(body)
显示了一个对象?它应该显示一个字符串。您可以尝试在request()选项中设置
json:true
。实际的堆栈跟踪是什么?是指向您的
JSON.parse(body)
的错误,还是来自
.send(datas)
内部的某个地方?所以我猜它来自JSON.parse,但为什么有时只会发生?我必须发送几次请求来重现这个bug。真正的问题是在我的计算函数中,我正在创建一个对象并处理正在创建(尚未创建)的字段,所以我使用了一个承诺来解决这个问题。你的回答是一个很好的提示,谢谢你。