Get object from node module

Get object from node module
0

#1

Hi, this regularly confuses me and I have just spent the last 45 minutes now trying to get something to work.

I have a bot that uses the Twitter API and I want to pull the accounts being followed by a given account and add those to an array to be worked on in another module, so what I have so far is:

getUsersList.js:

const Twit = require('twit')
const config = require('./config')

const bot = new Twit(config)

let users = []

const listOutFollowers = userName => {
  bot.get(
    'friends/ids',
    {
      screen_name: userName,
      count: 200
    },
    function getData(err, data, response) {
      if (err) {
        console.log(err)
        return
      }
      users.push(data.ids)
      if (data['next_cursor'] > 0) {
        bot.get(
          'followers/ids',
          {
            screen_name: userName,
            count: 200,
            next_cursor: data['next_cursor']
          },
          getData
        )
      }
    }
  )
}

module.exports = { users }

Then I want to be able to call it from another module:
bot.js:

const users = require('getUsersList.js')

// do something with array

I’m not able to get anything back from the other module


#2

All you’re doing is exporting an object with an empty array in it. The function listOutFollowers does not execute itself. What you’ll want to do is have that function return your array of users and then you would call that function from the module after it’s been required.

// in bot.js
const getFollowers = require('./getUsersList');

const users = getFollowers('someCoolPerson'); // returns an array