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
setTimeout(gotData, 10000);
invoke gotData without any parameters where asT.get('statuses/home_timeline', params, gotData);
invoke it passing retrieved data.Try following
You have to call the
GET
request again after the timeout, unless thegotdata
function is invoked without passing response data. Try something like this.Your
goData()
function expects three parameters:But you are calling it without any params in
T.get('statuses/home_timeline', params, gotData)
andsetTimeout(gotData, 10000)
, that’s why you will get this exception becausedata
isundefined
in: