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 Build a REST API With Express Completing and Testing the API Connecting the API and Database

PoJung Chen
PoJung Chen
5,856 Points

There is no 'err' parameters in `router.param('aID', callback)`

In the lecture, there is a code write like below:

router.param('aID', function (req, res, next, id) {

  req.answer = req.question.answers.id(id)   if (!req.answer) {
    err = new Error('Not Found')
    err.status = 404
    return next(err)
  }
  next()
})

However, there is no err parameter in the callback function. Is there anything I mistype?

Tom Geraghty
Tom Geraghty
24,174 Points

It might be more clear if, in the video, it was written like this:

var err = new Error('Not Found');

So people don't think the value is being passed in from elsewhere rather than being created in that instance (i.e.: when there is no req.answer).

1 Answer

Gerry K
Gerry K
5,877 Points

Hi,

The error is being created at:

err = new Error('Not Found')
err.status = 404
return next(err)

And the last line above is passing the error off to the Error Handler.

You would only be passing the err into a callback function into the error handler to let Node know that its the error handler.

If you look in the app.js file it has the error handler code which I've pasted below:

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