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
raul cortes
5,745 PointsVery lost with req.questions and req.answers
I am lost with these parameters req.questions and req.answers because they are not declared anywhere and teacher is posting and getting data with those.
Help please because i cant imagine how is data flowing.
Thx a lot
3 Answers
miikis
44,957 PointsHi Raul,
Like Simon says above, we need more information. But, if I had to guess, I'd say that req stands for request and res stands for response. Based on those assumptions, it's probable that you are looking at an event-handler of some kind for a HTTP message. Depending on whether you're client-side (e.g. doing an AJAX call) or server-side (e.g. building a router), the data is originating from the former and flowing to the latter, or vice-versa.
UPDATE
Dude, my bad. I didn't realize that your question was: where did the question and answer properties come from on the req object? I thought you were asking where the req object came from. In any case, the properties on the req object will come from one of two places: the client sent them to your server or you set them yourself inside of some ExpressJS middleware. Let me know if you need me to clarify further.
As a side note, I'm not sure I'd call adding properties to JavaScript objects via the dot-syntax a feature — it's just one of the main ways that properties are set in JavaScript. I'd argue that you should probably have a good understanding of rudimentary concepts like these before attempting to take a somewhat advanced course like the Express API course.
raul cortes
5,745 PointsHi Simon, this is the video that I'm talking about https://teamtreehouse.com/library/connecting-the-api-and-database (Sry for not telling what video was, in the ask options said that it would be related to the course)
Simon Coates
28,695 PointsI'm not familiar with the technology in question, SO I'M GUESSIN HERE but looking at the downloaded source code, they seem to be assigned in :
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;
return next();
});
});
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();
});
i think the param callbacks are guaranteed to run before routes that use them. I'd assume it's a mechanism to use the model and the request to provide some kind of complex object to the routes.
raul cortes
5,745 PointsI figured out what happened, I thougth that req.question was a predefined property of the req parameter, but it is created in the param method. It's a Javascript feature that allows you to add diferent properties to a Javascript object.
//for example
a={a:2,b:3};
a.c=4;
console.log(a)//that should prind a={a:2,b:3,c:4)
Obvioulsy I'm not an expert, but I tried this in the same code doing the following.
req.inventedProperty="this is my experiment".
console.log(req.inventedProperty);
//this printed the string in the node js console. So that's why about my conclusion.
Anyway, thank you a lot for your time :)
miikis
44,957 PointsI updated my answer up top :)
Simon Coates
28,695 Pointssorry that i didn't explain that you can add properties on the fly. Absent that bit of information, i get why my answer wasn't as helpful as I thought.
Simon Coates
28,695 PointsSimon Coates
28,695 Pointsyour question doesn't seem to have been attached to a video or a challenge. If you post a URL, then people will at least know what the question is about.