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 2017 Handling Errors in Node Handling the Error Event in Node

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

Not getting the proper error.

I added the code to line 33 to get an error output but I don't get an error just a blank line. Why is that?

// 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');


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

//function to get multiple usernames
function getProfile(username) {
//connect to API url (https://teamtreehouse.com/username.json)
  const request = https.get(`https://wwwcsscteamtreehouse.com/${username}.json`, response => {
       let body = "";
        //Read the json data                          
        response.on('data', data => {
          body += data.toString();
        });

        response.on('end', () => {
          //parse the data
          const profile = JSON.parse(body);
          //print the data
          printMessage(username, profile.badges.length, profile.points.JavaScript)                            
         });

    });

request.on('error', error => console.error(`Problem with request: ${error.message}`));

}

//process argv get arguments (users) from the command line
//slice leaves off the first two repsonses returned that arent usernames. 
const users = process.argv.slice(2);
users.forEach(getProfile);

2 Answers

Steven Parker
Steven Parker
231,275 Points

Did you provide an argument on the command line?

With no command line argument(s), the code that might generate the error never runs.

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

yeah.... that was the issue. A little embarrassing! thanks