orlando marinella orlando marinella - 1 year ago 92
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){

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

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

function initTwitter(){
console.log('Test!') //debug, triggers
console.log('Othertest!') //debug, triggers.
console.log(following) //debug, output is 'Undefined'.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download