Skip to content

Instantly share code, notes, and snippets.

@dorian-davis
Last active April 23, 2017 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dorian-davis/10da8a91d8bfcd8fcc4b9157579014bf to your computer and use it in GitHub Desktop.
Save dorian-davis/10da8a91d8bfcd8fcc4b9157579014bf to your computer and use it in GitHub Desktop.
Looping through pages of of users using Intercom-node SDK
var Intercom = require('intercom-client');
var client = new Intercom.Client(APP_ID, API_KEY);
var counter = 0;
function logUserNames(r) {
var users = r.body.users; // set users as the variable for the body > users part of the JSON response
// loop through all returned users and list their names
for(var i = 0; i < users.length; i++) {
counter++;
console.log(counter + ': ' + users[i].name);
}
// if not on the last page go to next page
if(r.body.pages.page !== r.body.pages.total_pages) {
// call nextPage function and call this method recursively until all pages have been iterated through
client.nextPage(r.body.pages, function(err, res) {
if(err) throw err;
logUserNames(res);
});
}
}
// list all users (r is the JSON response from the API)
client.users.list(function (err, r) {
if (err) throw err;
logUserNames(r);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment