Grace O'Brien
Grace O'Brien

Reputation: 121

Twitter stream.on Unexpected Token E

I am trying to set up a search button on my web application that retrieves all twitter tweets with the search input using the streaming API.

This is my client-side code: index.ejs

<form class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input class="form-control" id="searchValue" placeholder="Search" type="text">
      <button type="submit" class="btn btn-success" id="searchButton">
        <span class="glyphicon glyphicon-search"></span>
      </button>
  </div>
</form>

search.js

$(document).ready(function() {
    $("#searchButton").click(function(e) {
        e.preventDefault();
        var searchValue = $("#searchValue").val();
        $.ajax({
            url: "/submit",
            type: "POST",
            data: {
                searchValue: searchValue
            }
        });
     });
 });

And finally my server side code (using node.js/express)

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


router.post('/submit', function (req, res) {
  twit.stream('statuses/filter', {
    'track': [req.body.searchValue]
  }, function (stream) {
    stream.on('data', function (tweets) {
      var tweet = tweets.text;
    });
    stream.on('error', function (err) {
      console.log(err);
    });
  });
});

It works when I use the search button twice, however on the third time I enter a search it gives me the following error message:

[SyntaxError: Unexpected token E]

Thanks for any help in advance!

Upvotes: 0

Views: 964

Answers (1)

cleong
cleong

Reputation: 7636

Twitter is responding to the request with a "Exceeded connection limit for user" message. That's where the E comes from. The text is not a valid JSON object.

Upvotes: 1

Related Questions