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 the Error Event in Node

Jake Kobs
Jake Kobs
9,215 Points

Question about .on method

For the .on method below, when we use "error" as the first parameter, are we declaring what that variable does in the second parameter?

request.on("error", error =>{console.error("There was an error trying to process your request");})

2 Answers

Antti Lylander
Antti Lylander
9,686 Points

I'm new to node.js as well, but I'll try.

The first parameter in the double quotes is not a variable. It just identifies the event type that will trigger the callback function that is the second parameter. In other words. The callback function is run on response's "error" event. Just like "data" and "end". The callback function receives an error object as a parameter but you can name that anything, like 'err' or whatever.

If you are unfamiliar with the arrow function syntax, there is a short course about it in treehouse.

Jake Kobs
Jake Kobs
9,215 Points

Thank you Antti Lylander! That is kind of what I was thinking. Everything seems so tedious in the beginning, but we'll all manage :)

Antti Lylander
Antti Lylander
9,686 Points

I forgot one thing. In the example, the error object is not used in the error message at all in this case.

These are actually "arguments" supplied to the .on() method, not parameters. A parameter would be the variable inside the method declaration in the https object library. You are passing these arguments into the method to be used in the parameters in the function logic that is run "behind the scenes"

The first "error" is an argument passed in, and the logic in the .on() method will use that to determine the type of event you are trying to handle., and use it's logic within itself accordingly. We cannot see this logic, as .on() is a method on the https object we imported in our require() statement. You would have to go to the node.js docs, and find it to view the logic for the method.

The second argument is the entire callback function. The error variable here is the object supplied to the callback function from the .on() method. You could declare this as anything you want, i.e. err, e, foo, bar. Either way, the object itself does not change, it is still an error object. It just changes the variable name you use to reference the object within the callback function.'

Example:

// This is the same...
request.on("error", error =>{console.error(`There was an error trying to process your request: ${error.message}`);})
// as this...
request.on("error", foo =>{console.error(`There was an error trying to process your request: ${foo.message}`);})
// and this
request.on("error", (foo) =>{console.error(`There was an error trying to process your request: ${foo.message}`);})
// and this
request.on("error", function(foo){
    console.error(`There was an error trying to process your request: ${foo.message}`);
})
Harris Handoko
Harris Handoko
3,932 Points

Just what I needed, thanks. I didn't like the fact that he used the word 'error' on both the event listener type and the callback argument. Somehow I got "Problem with request: certificate has expired" as my error message, I wonder why....