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

Gillian Nieh
Gillian Nieh
5,709 Points

Invalid status code: undefined

Hello,

I am matching code in the videos, and I still can't get my cards route to work. Here is my cards.js code:

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

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


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

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

    const name = req.cookies.username;
    const text = cards[id][side];
    const { hint } = cards[id];

    const templateData = { id, text, name };
    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;

I believe the error is somewhere in here. I have also included my app.js code:

const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser')

const app = express();
app.use(bodyParser.urlencoded( {extended: false} ));
app.use(cookieParser());

app.set('view engine','pug');

const mainRoutes = require('./routes');
const cardRoutes = require('./routes/cards');

app.use(mainRoutes);
app.use('/cards', cardRoutes)

app.use((req, res, next) => {
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.use((err, req, res, next) => {
    res.locals.error = err;
    res.status(err.status);
    res.render('error', err);
});


app.listen(3000, () => {
    console.log('The application is running on localhost:3000!');
});