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

Solution working only halfway, but the code is the same in both cases..?

I tried to make the 'href' attribute as short as possible. It works when I want to see the answer, but doesn't work the other way around.

If I go back to the question, it brings me to http://localhost:3000/?side=answer instead.

Why?

cards.js

const express = require('express')
const router = express.Router()

const { cards } = require('../data/flashcardsData.json').data

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

    const templateData = (side == 'question') ? { id, side, text, hint } : { id, side, text }
    res.render('card', templateData)
})

module.exports = router

card.pug

extends layout.pug

block content
    section#content
        h2= text
        if hint
            p
                i Hint: #{hint}
        if (side==='question')
            //- this gets me to http://localhost:3000/cards/3/?side=answer which is expected
            a(href=`/?side=answer`) Show Answer
        else
            //- this gets me to http://localhost:3000/?side=answer which is not expected
            a(href=`/?side=question`) Show Question