0

I've tried a few variations of this snippet. I keep getting the same undefined error when trying to print a json object to the console. Thanks in advance.

var userTweets = client.get('statuses/user_timeline', function(error, tweets, response){
if(error) throw error;
return tweets
});

console.log(userTweets)
andrew_b
  • 29
  • 1
  • 4

1 Answers1

2

client.get is asynchronous

This code is working, then you have to manage your code to wait for asynchronous response.

client.get('statuses/user_timeline', function(error, tweets, response){
    if(error) throw error;
    console.log(tweets)
});

If you have some code after client.get, you should do that

before :

var userTweets = client.get(..., function() {});

// do some code
console.log(userTweets);

after :

client.get(....., function(error, tweets) {
   ...
   otherCode(tweets);
});

function otherCode(userTweets) {
   // do some code
   console.log(userTweets);
}

I suggest you to read this answer : https://stackoverflow.com/a/11233849/5384802 to understand asynchronous code.

Community
  • 1
  • 1
Arnaud Gueras
  • 2,014
  • 11
  • 14
  • Thanks, more so than logging to the console I really want the value of userTweets to be what client.get returns. – andrew_b Dec 16 '15 at 12:11