Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Foreach | For loop Javascript - Treehouse API extension. Issue iterating over the loop for pulling and printing an array

The issue is iterating through the array of users. Appreciate the support.

// Problem: We want to track a users progress each week and display their progress on a chart

// Solution: Use node.js to connect to Treehouse's API and get profile data to print out
var https = require("https");

var usernames = ["dillonkavanagh", "seantrant", "briankershaw", "beatrizbarcelos", "troyou", "dalalalshohaib", "matthewberrill", "stephenhackett", "kathleenkelly"];

// Print out message
function printMessage(username, badgeCount, points){
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in total.";

function printStats(usernames, points){
  var messageLoop = usernames + " has " + points + " points in total!";

// Print out error messages
function printError(error){

// Connect to the API URL's [Handle a database array of usernames] (https://teamtreehouse.com/dillonkavanagh.json
for(var i = 0; i <= usernames.length; i++){

  var request = https.get("https://teamtreehouse.com/" + usernames[i] + ".json", function(res){
  var body = "";
  // console.log(res.statusCode);

  // Read the data
  res.on('data', function(chunk){
    body += chunk;

  res.on('end', function(){
    if(res.statusCode === 200){
      try {

      var profile = JSON.parse(body);
      printStats(usernames[i], profile.points.total)

        } catch(error) {
          // parse error
    } else {
      // Status Code error
      printError({message: "There was an error getting the profile for " + usernames[i] +  ". (" + http.STATUS_CODES[res.statusCode] +") "});

  // Connection Error
  request.on("error", printError);

  // Store / Parse relevant data

  // Print data to screen