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 trialJenn Glas
9,475 PointsHelp with Node JS error handling
The question is:
Finally, in the error callback, use the error method on the console to print out the error message.
const http = require("http");
const request = http.get("http://teamtreehouse.com/chalkers.json", function(response){ console.log(response.statusCode); }); function printError(error) { console.error(error.message);
const http = require("http");
const request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
console.log(response.statusCode);
});
function printError(error) {
console.error(error.message);
2 Answers
Kevin Lassar
22,440 PointsHere was my solution for this task..
const https = require("https"); const request = https.get("https://teamtreehouse.com/chalkers.json", response => {
console.log(response.statusCode);
});
request.on('error', error => console.error(error.message));
You need to use the request.on method to listen for your functions error event. Then pass in your parameter error with a callback function to finally log your error message with the console.error(error.message).
Hopefully this helps! I'll admit I struggled myself on this too, so I apologize if my explanation is lacking in great enough detail.
Steven Parker
231,261 PointsYour last function is missing the close brace ("}
") at the end.
That appeared to the the only issue at first glance. But when I checked the challenge linked to this question, the code shown here is significantly different from the code that would pass the challenge at task 2. So either the link points to the wrong challenge, or a number of changes unrelated to the instructions were made after task 2?