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 Status Code Errors

George Matthews
George Matthews
8,029 Points

I can't figure out why i'm getting Undefined when running node app.js chalkers-not

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

//Print Error Messages
function printError(error) {
    console.error(error.messsage);
}


//Function to rpint 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/georgematthews3.json)
        const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
            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);
                        //Print the data
                        printMessage(username, profile.badges.length, profile.points.JavaScript);
                    } catch (error) {
                        printError(error);
                    }
                });
            } else {
                const message = `There was an error getting the profile for ${username} (${response.statusCode})`;
                const statusCodeError = new Error(message);
                printError(statusCodeError);
            }
        });
        request.on('error', printError);
    } catch (error) {
        printError(error);
    }
}
const users = process.argv.slice(2);
users.forEach(getProfile);

//const users = ["chalkers", "georgematthews3"];
//users.forEach (getProfile);
//getProfile("chalkers");
//getProfile("georgematthews3");
George Matthews
George Matthews
8,029 Points

Nevermind found it

function printError(error) {
    console.error(error.messsage);
}

message 2 s's