JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Linking Around the Application

Not Working

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

router.qet('/', (req, res) => { const numberOfCards = cards.length; const flashcardId = Math.floor(Math.random() * numberOfCards); res.redirect(/cards/${flashcardId}?side=question); });

router.get('/:id', function (req, res) { const { side } = req.query; const { id } = req.params;

if (!side) {
    res.redirect(`/cards/${id}?side=questiom`);
}

const text = cards[id][side];
const { hint } = cards[id];

const templateData = { id, text };

if (side === 'question') {
    templateData.hint = hint;
    templateData.sideToShow = 'answer';
    templateData.sideToShowDisplay = 'Answer';
} else if (side === 'answer') {
    templateData.sideToShow = 'question';
    templateData.sideToShowDisplay = 'Question';
}

res.render('card', templateData);

});

module.exports = router;

1 Answer

mersadajan
mersadajan
16,129 Points

You got a typo:

if (!side) {
    res.redirect(`/cards/${id}?side=questiom`);
}