javascript - events.js:85 throw er;Unhandled 'error' event - Stack Overflow

admin2025-04-19  0

I'm trying to setup a twitter app and I'm currently stuck on the following error:

$ node twitter.js
events.js:85
throw er; // Unhandled 'error' event
      ^
SyntaxError: Unexpected token U
    at Object.parse (native)
    at EventEmitter.receive (/Users/user/Documents/twitter/node_modules/twitter/lib/parser.js:40:21)
    at IningMessage.<anonymous> (/Users/user/Documents/twitter/node_modules/twitter/lib/twitter.js:207:14)
    at IningMessage.emit (events.js:129:20)
    at readableAddChunk (_stream_readable.js:163:16)
    at IningMessage.Readable.push (_stream_readable.js:126:10)
    at HTTPParser.parserOnBody (_http_mon.js:132:22)
    at TLSSocket.socketOnData (_http_client.js:317:20)
    at TLSSocket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
MBPro:twitter $ 

Synthax error? I don't understand.

Twitter.js code:

var tweet = require('twitter'),
    twitter = new tweet({
        consumer_key: '',
        consumer_secret: '',
        access_token_key: '',
        access_token_secret: ''
    });

var count = 0,
    util = require('util');

twitter.stream('filter', {track: 'test'}, function(stream){

    stream.on('data', function(data) {
        console.log(util.inspect(data));
        stream.destroy();
        process.exit(0);
    });

});

I'm trying to setup a twitter app and I'm currently stuck on the following error:

$ node twitter.js
events.js:85
throw er; // Unhandled 'error' event
      ^
SyntaxError: Unexpected token U
    at Object.parse (native)
    at EventEmitter.receive (/Users/user/Documents/twitter/node_modules/twitter/lib/parser.js:40:21)
    at IningMessage.<anonymous> (/Users/user/Documents/twitter/node_modules/twitter/lib/twitter.js:207:14)
    at IningMessage.emit (events.js:129:20)
    at readableAddChunk (_stream_readable.js:163:16)
    at IningMessage.Readable.push (_stream_readable.js:126:10)
    at HTTPParser.parserOnBody (_http_mon.js:132:22)
    at TLSSocket.socketOnData (_http_client.js:317:20)
    at TLSSocket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
MBPro:twitter $ 

Synthax error? I don't understand.

Twitter.js code:

var tweet = require('twitter'),
    twitter = new tweet({
        consumer_key: '',
        consumer_secret: '',
        access_token_key: '',
        access_token_secret: ''
    });

var count = 0,
    util = require('util');

twitter.stream('filter', {track: 'test'}, function(stream){

    stream.on('data', function(data) {
        console.log(util.inspect(data));
        stream.destroy();
        process.exit(0);
    });

});
Share edited May 15, 2015 at 15:19 E_net4 30.1k13 gold badges114 silver badges151 bronze badges asked May 15, 2015 at 14:55 sslsssslss 3472 gold badges4 silver badges12 bronze badges 4
  • You'll need to include some of your code or data to debug this. It looks as though you're parsing some object, so the data will probably be significant, along with the library used to parse. – ssube Commented May 15, 2015 at 14:56
  • Sure. I was going to but it was too much code in one post for SO. – sslss Commented May 15, 2015 at 15:01
  • @sslss I agree, it is very hard to provide adequate help witch such little info BUT you can check /Users/user/Documents/twitter/node_modules/twitter/lib/parser.js line 40 it is JSON.parse(json) and the error must be that the expected json response is not actually valid JSON. Just before line 40 put console.log('What is this?', json); to see from console what the response actually is. This may clear the situation a bit. – tiblu Commented May 15, 2015 at 15:39
  • @tiblu Thanks for the tip! What is this? Unknown URL. See Twitter Streaming API documentation at http://dev.twitter./pages/streaming_api – sslss Commented May 15, 2015 at 16:17
Add a ment  | 

2 Answers 2

Reset to default 2

This is a bug in the twitter module. The module should be checking the HTTP response's Content-Type header value before trying to parse the response as JSON.

Here is the relevant issue on github.

I was getting the same error and it was because by mongodb instance was not running. I started mongodb in my osx with;

sudo mongod
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745054383a282308.html

最新回复(0)