简体   繁体   English

Node.JS上的JSON.Parse不会每次都起作用

[英]JSON.Parse on Node.JS will not work everytime

Here is my code : 这是我的代码:

var req = https.request(options, (res) => {

  res.on('data', (d) => {
    var data = JSON.parse(d);

    >> REDIS ACTION <<
    }
  });
});
req.end();

I assume that the JSON may be, and is most of the time large, but i don't understand this error : 我认为JSON可能是而且在大多数情况下都是很大的,但是我不明白这个错误:

    >> JSON DATA TRUNCATED, AND THIS DON'T HAVE TO BE DISPLAYED <<

SyntaxError: Unexpected end of input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (A:\p
    at emitOne (events.js:77:13)
    at IncomingMessage.emit (events.js:1
    at readableAddChunk (_stream_readabl
    at IncomingMessage.Readable.push (_s
    at HTTPParser.parserOnBody (_http_co
    at TLSSocket.socketOnData (_http_cli
    at emitOne (events.js:77:13)
    at TLSSocket.emit (events.js:169:7)

Can you explain me where is my fault and how to handle it ? 您能解释一下我的错在哪里以及如何处理吗?

As cdbajorin said, i needed to compose my data, because this occurs when node try to execute a command, but all the request isnt't already received. 正如cdbajorin所说,我需要编写数据,因为这是在节点尝试执行命令时发生的,但尚未收到所有请求。

Here is the code who solved my issue: 这是解决我的问题的代码:

var req = https.request(options, (res) => {
  var response = '';

  res.on('data', (d) => {
    response += d;
  })

  res.on('end', () => {
    var data = JSON.parse(response);

    //EVERYTHING ELSE
      });
    }
  });
});
req.end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM