orlando marinella orlando marinella - 5 months ago 18
Javascript Question

Unknown Twitter API issue

Right, so.

I'm trying to grab a list of friends from a given screen_name using the 'Twitter' module (found on npm, installed using

npm install twitter
), however, my program keeps failing, and I'm not sure why, exactly. At this point in time, all it's supposed to do is rattle off the list of the friends, contained within the friends JSON object, but that doesn't work, for some reason.

I can't tell why. The code is contained below. Please leave a comment if you need to ask a question.

function readTokenFromDisc(){ //Used to save on requests.
return new Promise(function(resolve, reject){
fs.readFile('bearerToken.txt', 'utf8', function(error, data){
resolve(data);
});
});
}

function buildClient(bToken){
return new Promise(function(resolve, reject) {
var client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
bearer_token: bToken
});
resolve(client);
});
}

function grabTwitterFollowers(client){
return new Promise(function(resolve, reject) {
client.get('friends/list', {screen_name: 'POTUS'}, function (error, tweets, response){
if(error){
console.log(error);
};
resolve(tweets);
console.log(tweets) //debug
console.log(response) //debug
});
});
};

function initTwitter(){
console.log('Test!') //debug, triggers
readTokenFromDisc().then(function(contents){
console.log('Othertest!') //debug, triggers.
buildClient(contents);
}).then(function(client){
grabTwitterFollowers(client);
}).then(function(following){
console.log(following) //debug, output is 'Undefined'.
});
}

Answer

You're missing returns.

grabTwitterFollowers(client); and buildClient(contents);

in initTwitter should probably be

return grabTwitterFollowers(client); and return buildClient(contents);

Since that returns a promise for the chain. Otherwise, the promise will pass along undefined, the result of a function without a return.

Comments