node.js SyntaxError:Object.parse(本机)npm 请求中的输入意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29259395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected end of input at Object.parse (native) npm request
提问by Fran?ois Richard
Hello I don't understand why I have this error, I thought the callback was executed once the data has been received, any idea where this come from ? Thanks a lot!
您好,我不明白为什么会出现此错误,我以为收到数据后就会执行回调,知道这是从哪里来的吗?非常感谢!
Node error:
节点错误:
SyntaxError: Unexpected end of input
at Object.parse (native)
I parse the body's answer send it to a calculate function before sending it to the page =/
我解析正文的答案,然后将其发送到计算函数,然后再将其发送到页面 =/
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);
Here is the stack trace:
这是堆栈跟踪:
'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...
回答by Chris
As discussed in the comments, you are probably getting empty or malformed requests which cause the JSON.parseto throw. Something like this should help you:
正如评论中所讨论的,您可能会收到导致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);
}

