2015-03-25 6 views
5

Ciao Non capisco perché ho questo errore, pensavo che la richiamata fosse eseguita una volta che i dati sono stati ricevuti, qualsiasi idea da dove provenga? Grazie mille! Errore diSintassiErrore: Fine imprevista dell'input alla richiesta npm Object.parse (nativa)

Node:

SyntaxError: Unexpected end of input 
    at Object.parse (native) 

il parsing la risposta del corpo inviarlo a una funzione di calcolo prima di inviarlo alla pagina =/

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); 

Ecco la traccia dello stack:

'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... 
+0

Cosa mostra 'console.dir (corpo)'? – mscdex

+0

mostra l'oggetto, questo è strano, ma funziona a volte non si riesce a capire perché –

+0

'console.dir (corpo)' mostra un * oggetto *? Dovrebbe mostrare una stringa. Potresti provare a impostare 'json: true' nelle tue opzioni request(). – mscdex

risposta

12

Come discusso nei commenti, probabilmente stai ricevendo richieste vuote o malformate che causano lo JSON.parse da buttare. Qualcosa di simile dovrebbe aiutarti:

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); 
} 
+0

il vero problema era nella mia funzione di calcolo, stavo creando un oggetto e lavorando sui campi che si stavano creando (non ancora creati), quindi ho risolto questo usando una promessa. La tua risposta è comunque un buon consiglio, grazie. –