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 trialjohnywick
3,231 Pointsconst templateData = { id, text }; need explanation for this . why put id ?
const templateData = { id, text }; need explanation for this . why put id ?
3 Answers
Steven Parker
231,141 PointsThe brackets create what is known as an object literal, which is then assigned to the variable "templateData". This object has an "id" property and a "text" property to start with (more properties are added in the following code). The properties are to be used by the "render" method later to create the view.
johnywick
3,231 PointsThank you very much.
Steven Parker
231,141 Pointsjohnywick — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!
johnywick
3,231 Pointsrouter.get("/:id", (req, res) => {
const test = req.query.test; // req.query.side //side =question
const id = req.params.id; //req.params.id
if (!test) {
res.redirect(`/cards/${id}?test=question`);
}
const text = cards[id][test]; // cards[0].question
const { hint } = cards[id]; // data.cards[0].hint
const templateData = { id, text };
console.log(templateData);
if (test === "question") {
// if req.query === 'quesiton'
templateData.hint = hint;
console.log(templateData.hint);
templateData.sideToShow = "answer";
templateData.sideToShowDisplay = "Answer";
} else if (test === "answer") {
templateData.sideToShow = "question";
templateData.sideToShowDisplay = "Question";
}
res.render("card", templateData);
});
johnywick
3,231 PointstemplateData = { id, text } i dont understand this why id in braket .
johnywick
3,231 Pointsconst templateData = { id, text }; how to read this kind of coding. what its name?
Steven Parker
231,141 PointsSteven Parker
231,141 PointsWe'd need a bit more context to determine the purpose of this little snippet. Can you provide a link the the course page this is from, and/or more of the code?