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 trialAndrea Mogren
Full Stack JavaScript Techdegree Student 8,096 PointsWhy is 'message' in PrintError console.error(error.message) undefined?
I've tried to work it out but I don't understand why 'message' in PrintError console.error(error.message) undefined? What am I doing wrong? Here 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
const https = require('https');
//Print Error Messages
function printError(error) {
console.error(error.message);
}
try {
//Print message to console
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} total badge(s) and ${points} in JavaScript`;
console.log(message);
}
function getProfile(username) {
//Connect to the API URL (https://teamtreehouse.com/username.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
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 out the data
printMessage(username, profile.badges.length, profile.points.JavaScript);
} catch (error) {
printError(error);
}
})
request.on('error', printError());
})
}
} catch(error) {
printError(error);
}
const users = process.argv.slice(2);
users.forEach(getProfile);
2 Answers
nico dev
20,364 PointsHi Andrea Mogren ,
I think it's just this line:
request.on('error', printError());
Remember, when a function is passed as an argument, you don't want to call it, therefore you don't use the parentheses that we use when calling it.
Thus, it should be:
request.on('error', printError);
If not for that, I think it's super fine. Check it out and follow up here if smth still goes wrong. :)
Andrea Mogren
Full Stack JavaScript Techdegree Student 8,096 PointsYes now it's working! Thank you for your help and your explanation Nico! :)
nico dev
20,364 Pointsnico dev
20,364 PointsJust to expand a little on that, I think the printError is called before any error exists (or regardless of its existence or the lack of it), so since the error is undefined (the error object is still empty of any values), the message property is also undefined, empty.
Hope that clarifies a bit what's going on with the error (pardon the pun!) you receive.