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

Why 'err.status = 404' and 'err.status(404)' are both used?

err.status = 404;

vs

err.status(404);

It seems like you'd want to pick one way or the other for consistency in code style.

No explanation on why in one part of the video Andrew used the first way and in another part used the second way of defining the status before passing the error on to the next() function.

Were you showing us there are two ways of doing the same thing? Why would you pick one over the other? Or are they exactly the same?

2 Answers

Steven Parker
Steven Parker
242,783 Points

A definitive answer would require seeing a link to the course page and/or more of the code for context, but I might guess that these things are being done to different objects (of different types) that happen to have the same name.

For example, "err.status = 404;" might be a case where a new status property is being added to an object that did not have one before (perhaps one newly created); and "err.status(404);" could be a middleware method call on an argument passed to a use method.

Hmm, for whatever reason the video on which I asked the question did not get linked properly to the question. It's from the Build a REST API with Express course (https://teamtreehouse.com/library/next-steps-2).

I downloaded and unzipped the project files, navigated to qa-rest-api-completed and opened the code there in my editor to see:

// catch 404 and forward to error handler
app.use(function(req, res, next){
    var err = new Error("Not Found");
    err.status = 404;
    next(err);
});

// Error Handler

app.use(function(err, req, res, next){
    res.status(err.status || 500);
    res.json({
        error: {
            message: err.message
        }
    });
});

In seeing this now rather than as it was quickly going by in the video, I am realizing that the call in the first is err.status = 404 whereas in the second middleware function it is using the response object like this res.status(500)

Maybe that answers my question? In one case the Error object is having it's property "status" set to a value and then the error is passed onto the error handler using next. In the other case, the response object is having it's "status" method called to set the property to 500.

Would it work to write it the other way. EG:

res.status = err.status || 500;

I'm not seeing in the MDN on JS' Error has a method called status(). So it likely would not work to write:

err.status(500);

Let me know if I am misunderstanding something after this further review. Thanks all!

Steven Parker
Steven Parker
242,783 Points

It sounds like you got it. Plus, you could not have a scalar property and a method with the same name on the same object.