skip to Main Content

I’m trying to get the last 10 tweets from my twitter. I have my keys in a separate file and require it at top. I followed the steps that are on npm twitter and everything seem to look right. When I run it in my terminal I don’t get any error or anything. Not too sure what is going on. This is the first time using the twitter api and still learning about node. Not sure is this is a problem but I notice that with I do “var client = new Twitter(twitterKeys);” the word Twitter is white and doesn’t turn another color like when calling on a method.

 var keys = require("./key.js");

 var Twitter = require('twitter');
 var twitterKeys = keys.twitterKeys;    

 function twitter() {
     var client = new Twitter(twitterKeys);

     var params = {screen_name: 'stacysareas', count: 10};

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

2

Answers


  1. I believe your error is here:

    if (!error) {
       console.log(tweets);
    }
    

    try this instead:

    if (error) {
       throw error;
    } else {
       console.log(tweets);
    }
    

    I believe that no matter what, something is coming back as the error argument. So when you say if !error, that condition never fires. Handle the error if it happens, and if not do something with the tweets

    Login or Signup to reply.
  2. It return very well, are you put it in get/post method of node? You need install Express as well

    router.get('/twitter', function (req, res, next){
    
    var params = {screen_name: 'stacysareas', count: 10};
    client.get('statuses/user_timeline', params, function(error, tweets, response) {
        if (!error) {
            console.log(tweets);
        }
    });})
    
    var client = new Twitter({
    consumer_key: 'xxx',
    consumer_secret: 'yyyy',
    access_token_key: 'zzzz',
    access_token_secret: 'zz'});
    

    Make sure you got right token from: https://apps.twitter.com/app/1466387/keys

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