thedarklord47
thedarklord47

Reputation: 3312

"SyntaxError: Unexpected token U" using node.js twitter module

This is apparently a bug in the twitter module as noted by this answer: https://stackoverflow.com/a/30264645

Despite this being a known issue, I have have been Googling for hours to no avail. Does anyone know of a work around? My issue results from JSON.parse being called on non-JSON data within the twitter module.

Here is the full trace:

events.js:85
  throw er; // Unhandled 'error' event
        ^
SyntaxError: Unexpected token U
at Object.parse (native)
at EventEmitter.receive (c:\Users\Cole\Development\NodeJS\example\node_modules\twitter\lib\parser.js:41:21)
at IncomingMessage.<anonymous> (c:\Users\Cole\Development\NodeJS\example\node_modules\twitter\lib\twitter.js:207:14)
at IncomingMessage.emit (events.js:129:20)
at readableAddChunk (_stream_readable.js:163:16)
at IncomingMessage.Readable.push (_stream_readable.js:126:10)
at HTTPParser.parserOnBody (_http_common.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)

Inserting console.log(json) into parser.js just before JSON.parse() (the second call of the trace stack) results in printing the json to be parsed.

This is what I get:

Unknown URL. See Twitter Streaming API documentation at http://dev.twitter.com/pages/streaming_api

Clearly not JSON and explains the error. But why am I getting this?

My node code (twitter.js):

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

twitter.stream('filter', {track: 'filter-term'}, function(stream){ 
    console.log('help!');
});

Both node twitter.js and node twitter produce the above error. What could be causing this "Unknown URL..." string to be where my JSON should be?

Upvotes: 2

Views: 1972

Answers (1)

thedarklord47
thedarklord47

Reputation: 3312

Figured it out.

twitter.stream('filter', {track: 'filter-term'}, function(stream){ ... });

should be

twitter.stream('statuses/filter', {track: 'filter-term'}, function(stream){ ... });

Least helpful error message ever...

Upvotes: 4

Related Questions