Last active
April 23, 2017 22:24
-
-
Save dorian-davis/10da8a91d8bfcd8fcc4b9157579014bf to your computer and use it in GitHub Desktop.
Looping through pages of of users using Intercom-node SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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