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 trialBrian Patterson
19,588 PointsGetting "undefined" in the console.
I am following along with this video but with another API. The problem I am having is that I am getting undefined in the console if the season is not correct. Below is my code.
const http = require('http');
//const season = 1954;
function printMessage(races, season) {
const message = `There are ${season} grandprix at the ${races} season.`;
console.log(message);
}
//Print Error Messages
function printError(error) {
console.error(error.messsage);
}
//printMessage(22,2017);
//Connecting to the API url (http://ergast.com/api/f1/2012.json)
function getSeason(season) {
try {
const request = http.get(
`http://ergast.com/api/f1/${season}.json`,
response => {
if (response.statusCode === 200) {
//console.dir(response.statusCode);
let body = '';
//Read the data
response.on('data', data => {
//Changing the buffer into a string.
body += data;
});
response.on('end', () => {
try {
//Parse the data
const seasonF1 = JSON.parse(body);
// console.log(body);
// console.log(typeof body);
//console.dir(seasonF1);
printMessage(season, seasonF1.MRData.RaceTable.Races.length);
//Print the data
} catch (error) {
printError(error);
}
});
} else {
const message = `There was an error getting the profile for ${season} (${
http.STATUS_CODES[response.statusCode]
})`;
const statusCodeError = new Error(message);
printError(statusCodeError);
}
}
);
request.on('error', printError);
} catch (error) {
printError(error);
}
}
//accept season as a command line argument
//const season = process.argv[2];
//get(season);
// get(1978);
// get(2001);
//console.log(process.argv);
// const seasons = [1978, 1984, 1999, 1950, 1954, 2002];
// seasons.forEach(getSeason);
const seasons = process.argv.slice(2);
seasons.forEach(getSeason);
Any help would be appreciated.
2 Answers
Joel Bardsley
31,249 PointsHi Brian, messsage needs to be corrected in the following:
//Print Error Messages
function printError(error) {
console.error(error.messsage);
}
Steven Parker
231,275 PointsWhat do you mean by "if the season is not correct", and what behavior do you expect in such a case?
For any year I tried, I got a message giving me the number of grandprix in that season.
Brian Patterson
19,588 PointsWhat I mean is if I put "node seasonF1.js 1956not" in the console it gives me an "undefined" error message.
Steven Parker
231,275 PointsI think it's what Joel discovered .. the "messsage" with 3 "s" characters. I didn't see it because I didn't have any errors.
Brian Patterson
19,588 PointsBrian Patterson
19,588 PointsSorry, I don't understand. What needs to change?
Joel Bardsley
31,249 PointsJoel Bardsley
31,249 PointsThe typo - message is misspelled (3 x 's'), the error reporting will be looking for the error property of message, so this is why it'll be showing up as undefined.