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 Express Basics Parameters, Query Strings, and Modularizing Routes Randomize Cards

my solution

Card.pug

extends layout.pug

block content
    section#content 
        h2= text
        if hint
            p
                i Hint: #{hint}
        a(href=`/cards/${id}?side=${link}`) #{link}

cards.js

const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
const { cards } = data;

router.get('/:id', (req, res) => {
    const {side} = req.query;
    const { id } = req.params;
    const text = cards[id][side];
    let templateData = { id, text};

    if( side === 'question' ){
        templateData.hint= cards[id].hint;
        templateData.link = 'answer';
    } else {
        templateData.link = 'question';
    }



    res.render('card', templateData);
});

module.exports = router;
Simon Coates
Simon Coates
8,177 Points

For anyone's future reference, I skipped string interpolation and just fed a url in with res.locals.url = req.baseUrl+req.path+"?side=answer"; Seemed to work at a cursory glance. I'm not sure if it makes perfect sense, but I think avoiding hardcoding a path in the view might make sense if you alter the path parameters for the middleware.

1 Answer

Trevor Maltbie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,020 Points

My understand of this deconstruction is that const templateData = {id, text}; is the same as

const templateData = {
id: id,
text: text
}

So, when we do templateData.link = 'answer'` is that like adding on to the above:

const templateData = {
id: id,
text: text
link: 'answer'
}

?