5

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...
7
  • What does console.dir(body) show? Commented Mar 25, 2015 at 15:16
  • it shows the object, this is weird this is working but sometimes it crashes can't tell why Commented Mar 25, 2015 at 15:43
  • console.dir(body) shows an object? It should show a string. You could try setting json: true in your request() options. Commented Mar 25, 2015 at 15:49
  • What is the actual stack trace? Is the error pointing to your JSON.parse(body) or is it coming from somewhere inside .send(datas) ? Commented Mar 25, 2015 at 15:54
  • So I guess it comes from the JSON.parse but why does it occurs sometimes only ? I have to send the request few times to reproduce the bug. Commented Mar 25, 2015 at 16:02

1 Answer 1

13

As discussed in the comments, you are probably getting empty or malformed requests which cause the JSON.parse to throw. Something like this should help you:

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

the real problem was inside my calculate function, I was creating an object and working on fields that was being created (not created yet) so I resolved this using a promise. Your answer is a good tip anyway thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.