Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics (2014) Building a Command Line Application Handling Parsing and Status Code Errors

Fareez Ahmed
Fareez Ahmed
12,728 Points

There was an error getting the profile for chalkers. (OK)

Why isn't my code working? It should return the username/points/ etc, but I'm getting the following error message: There was an error getting the profile for chalkers. (OK)

//Problem: simple way to view badge count/js points
//Solution: Use Node.js to connect to Treehouse's API to get profile information to print out
var http = require("http");
var username = "chalkers";
//Print out message
function printMessage(username, badgeCount, points){
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript";
  console.log(message)
}
//Print out error messages
function printError(error){
console.error(error.message);
}
//Connect to the API URL (https://teamtreehouse.com/username.json)
  //use http.get
var request = http.get('http://teamtreehouse.com/' +username+ '.json', function(response){
//console.dir(response);
//console.log(response.statusCode);
var body = "";
  //Read data
  response.on('data', function(chunk){
   body += chunk;
  });
  response.on('end', function(){
    if(response.StatusCode===200){
    try{
    var profile = JSON.parse(body);
    printMessage(username, profile.badges.length, profile.points.Javascript)
    } catch(error){
      //Parse Error
      printError(error);
    }
    }
    else{
    //Status Code Error
      printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
    }
  });
//parse data
//print data

});

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

Andrew Chalkley

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

The line:

if(response.StatusCode===200){

should be:

if(response.statusCode===200){

You have status capitalized.

Also, on line:

printMessage(username, profile.badges.length, profile.points.Javascript)

should be:

printMessage(username, profile.badges.length, profile.points.JavaScript)

You need to capitalize the 's' in 'JavaScript'.

That should do it. :)

Sean you beat me to this answer by 1 minute! On the other hand, you saved me the time from having to type out the correction! ;-)

Fareez Ahmed
Fareez Ahmed
12,728 Points

Thanks Sean T. Unwin! and Alex Friant it's the thought that counts! This was a great course btw, learned a lot.