skip to Main Content

I am trying to load the results of a GET request from the twitter api in a nodejs file held on AWS. Currently the code runs once and console.log’s the feed when loading the file in the terminal but when I use setTimeout to try to refresh the results i get .length undefined.

Currently my code looks like this…

var Twit = require('twit');
var config = require('./config.js');
var T = new Twit(config);

var params = {
  exclude_replies: true,
  count: 20,
  include_entities: false
};

T.get('statuses/home_timeline', params, gotData);

function gotData(err, data, response) {

  var timeLine = data;
  for (var i = 0; i < timeLine.length; i++) {
var obj = timeLine[i];

console.log(obj.user.name);
  };
};

setTimeout(gotData, 10000);

I have reviewed other questions relating to setInterval and .length issues but I’ve been unable to find anything I was able to apply to this issue

Thanks for any help you can give me

3

Answers


  1. setTimeout(gotData, 10000); invoke gotData without any parameters where as T.get('statuses/home_timeline', params, gotData); invoke it passing retrieved data.

    Try following

    setTimeout(function() {
        T.get('statuses/home_timeline', params, gotData);
    }, 10000);
    
    Login or Signup to reply.
  2. You have to call the GET request again after the timeout, unless the gotdata function is invoked without passing response data. Try something like this.

    setTimeout(function(){
        T.get('statuses/home_timeline', params, gotData);
    }, 10000);
    
    Login or Signup to reply.
  3. Your goData() function expects three parameters:

    function gotData(err, data, response)
    

    But you are calling it without any params in T.get('statuses/home_timeline', params, gotData) and setTimeout(gotData, 10000), that’s why you will get this exception because data is undefined in:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search