node.js流请求中的json输出无效

时间:2019-03-08 00:37:21

标签: node.js twitter request

我尝试使用request模块使用node.js创建一个简单的twitter流光,但我遇到了一些问题,结果有时是无效的JSON格式,有什么想法可以解决吗?

这是我的代码:

var request = require('request');


var oauth = {
  consumer_key: 'xxxx',
  consumer_secret: 'xxxx',
  token: 'xxxx',
  token_secret: 'xxxx'
  }

function filter(params, callback){
  var url = 'https://stream.twitter.com/1.1/statuses/filter.json';
  request
    .post({url:url, oauth:oauth, qs:params})
    .on('data', function(buffer){
      if (buffer) {
        return callback(JSON.parse(buffer.toString()));
      }
  });
};

filter({track: 'BTS' }, 
  function(tweet) {
    console.log(tweet.text)
  }
);

错误日志:

G:\ APP \ node>节点stream.js

RT @vendoBT21: (((EVENTO DE RT))) Estarei recebendo pedidos de produtos LINE FRI
ENDS BT21 originais que serão trazidos diretamente da Corei.
undefined:1
{"created_at":"Fri Mar 08 00:21:49 +0000 2019","id":1103813034454196226,"id_str":"1103813034454196226","text":"RT @i_uendelig_tid: It kills me how their outroscover such a wide range of styles and yet their quality remains consistently impeccable, f\u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1102269320309231616,"id_str":"1102269320309231616","name":"soobinie\/\/ adama is gonna see bts again \ud83e\udd70","screen_name":"kthmonie","location":"Up seokjin\u2019s ass","url":null,"description":"\ud83c\uddf8\ud83c\uddf1|| taejinkook","translator_type":"none","protected":false,"verified":false,"followers_count":46,"friends_count":94,"listed_count":0,"favourites_count":1453,"statuses_count":206,"created_at":"Sun Mar 03 18:07:39 +0000 2019"

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Request.<anonymous> (G:\APP\node\stream.js:
19:21)
    at Request.emit (events.js:187:15)
    at IncomingMessage.<anonymous> (G:\APP\node\no
de_modules\request\request.js:1080:12)
    at IncomingMessage.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at IncomingMessage.Readable.push (_stream_readable.js:219:10)
    at HTTPParser.parserOnBody (_http_common.js:122:22)
    at TLSSocket.socketOnData (_http_client.js:442:20)

0 个答案:

没有答案