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

Erik L
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik L
Full Stack JavaScript Techdegree Graduate 19,470 Points

node.js confused??

Hi I am currently doing this video, https://teamtreehouse.com/library/handling-status-code-errors, the instructor has added the following: const message = There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]});, when I try to load a fake profile like node Project.js chalkers-not, I do't get the error message in the console, can someone please help?

this is my code

// Problem: We need a simple way to look at a user's badge count and JavaScript points
// Solution: Use Node.js to connect to Treehouse's API to get profile information to print out

//Require https module
const https = require('https')

//Require https module FOR STATUS CODE
const http = require('http')

//Print error messages 
function printError(error) {
  console.error(error.message);
};


//Function to print message to console
function printMessage(username, badgeCount, points) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript`;
  console.log(message);
}

function getProfile(username) {
try {

 //Connect to the API URL (https://teamtreehouse.com/username.json)
  const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
  console.log('the status code is ' + response.statusCode);

//Handle responses that are not 200
if (response.statusCode === 200) {
    let body = '';

    //Read the data
    response.on('data', data => {
    body += data.toString();
    });
    response.on('end', () => {
      try {
      //Parse the data
    const profile = JSON.parse(body);
    printMessage(username, profile.badges.length,
      profile.points.JavaScript);
    } catch (error) {
      printError(error);
    }
      //Print the data
    });
} else {
  const message =  `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
  const statusCodeError = new Error(message);
  printError(statusCodeError);
}

  });
//this object has an error object passed into it 
  request.on('error',printError);
   } catch (error) {
    printError(error);
   }
  };

  /*
  ForEach iterates over the array and passes each member into a callback
  function, each member would be a username 
  */
  console.log(process.argv);
  const users = ["chalkers","alenaholligan", "davemcfarland"];
  users.forEach((username) => {
    getProfile(username);

 });  

1 Answer

Hi

The reason your not getting an error is because all of the 3 you have chosen are in fact profiles.

i have a file called test.js and in that file i run your code with the 3 following users:

const users = ["chalkers","alenaholligan", "davemcfarland"];

and here is the output when i run "node test.js":

the status code is 200
the status code is 200
alenaholligan has 129 total badge(s) and 142 points in Javascript
davemcfarland has 289 total badge(s) and 8314 points in Javascript
the status code is 200
chalkers has 209 total badge(s) and 5979 points in Javascript

now lets change to a profile we know for certain isnt in the datbase:

  const users = ["wellThisNameCantBeAProfile","alenaholligan", "davemcfarland"];

or

  const users = ["chalkers-not","alenaholligan", "davemcfarland"];

and heres the results

the status code is 404
There was an error getting the profile for wellThisNameCantBeAProfile (Not Found)
the status code is 200
the status code is 200
alenaholligan has 129 total badge(s) and 142 points in Javascript
davemcfarland has 289 total badge(s) and 8314 points in Javascript


or 

the status code is 404
There was an error getting the profile for chalkers-not (Not Found)
the status code is 200
alenaholligan has 129 total badge(s) and 142 points in Javascript
the status code is 200
davemcfarland has 289 total badge(s) and 8314 points in Javascript

so your code works as expected.

Im not sure as to the scope of the video but if you are interested, you could try refactor this, at the moment its a bit all over the place.

Maybe take a look at how you can have an ajax function that handles the request and its status in one place, that way your not only making your code more readable, but also making your code reusable for any ajax call you need to make.

Let me know if you get stuck

Good Luck