JavaScript Node.js Basics Handling Errors in Node Handling Errors in Node

Help 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);

app.js
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

Here 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
Steven Parker
203,733 Points

Your 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?