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

Eli Levit
Eli Levit
4,482 Points

req.question/req.answer

Everything works very well, but I didn't understand what req.question and req.answer are? Can you explain me please?

The answer has been provided, but I just wanted to add that I too was confused about this. Can someone please tell Andrew to stop skipping over details like this? It happens a lot.

1 Answer

nico dev
nico dev
20,364 Points

Hi Eli Levit ,

Good question.

Well, they are assigned/defined at the top of the routes.js file. More precisely here:

router.param('qID', function(req, res, next, id) {
    Question.findById(id, function(err, doc) {
        if (err) return next(err)
        if (!doc) {
            err = new Error('Not Found')
            err.status = 404
            return next(err)
        }
        req.question = doc  // Here is where req.question is defined.
        return next()
    })
})

router.param('aID', function(req, res, next, id) {
    req.answer = req.question.answers.id(id)   // Here is where req.answer is defined
    if (!req.answer) {
        err = new Error('Not Found')
        err.status = 404
        return next(err)
    }
    next()
})

What does that mean? Well:

First of all the question and answer properties are created there and assigned as properties to the request object (so you can use them in different routes without assigning them every single time and all of that, now you can just use them as properties: req.question or req.answer and that's it). Secondly, they're assigned their value depending on other stuff. Let's decompose it a little bit:

/****req.question*************/

  • req.question is doc (req.question = doc)...

  • ... where doc is the document (in the mongoDB) found when searching by ID. (Question.findById(id, function(err, doc))

  • Which ID? The ID of the question document, provided on the first line as an arg. (function(req, res, next, id))

So req.question is the question of the current request.

/********req.answer*************/

  • req.answer is, precisely, the answer

  • -> within the answers collection (req.answer = req.question.answers.id(id))

  • -> within that respective question document (req.answer = req.question.answers.id(id))

  • -> that has the id (req.answer = req.question.answers.**id(id)**)

  • -> provided in the args (function(req, res, next, **id**))

Hope that clear things a bit, but if still unclear, feel free to follow up here.

Tom Geraghty
Tom Geraghty
24,174 Points

Great answer, @nicotrivelli

I just wanted to add a link to the docs so you can read more: http://expressjs.com/en/api.html#app.param

App.param (or router.param) is basically a middleware function that adds a variable and value to the req object so it can be used in other middleware and routes in your application.

mulgey
mulgey
9,899 Points

Great answer, i needed to ask; but it was asked and replied so well that it resolved quickly.