node.js – SyntaxError:Object.parse(native)npm请求的意外输入结束

您好我不明白为什么我有这个错误,我认为回调是在收到数据后执行的,任何想法来自哪里?
非常感谢!

节点错误

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...

解决方法

正如评论中所讨论的那样,您可能会得到空的或格式错误的请求,导致JSON.parse抛出.这样的事情可以帮助你:

var callback = function(error,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);
}

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...