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

jason limmm
jason limmm
8,004 Points

h2 text not appearing

my h2 text isn't appearing i don't know why

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 mainroute=require('./routes');
const cardroutes=require('./routes/card/cards')

app.use(mainroute);
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');
});

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

card.js

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

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

    const templateData = {text, hint};
    res.render('card', templateData);
});

module.exports = router;

card.pug

extends layout.pug

block content
  section#content
    h2= text
    if hint
      p
        i Hint: #{hint}

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey jason limmm 👋

What route are you visiting to test this your code? You'll want to make sure that there is a query parameter present for the side as Andrew explains around the 1:37 minute mark

When I paste your code into the provided project files everything seems to be working as expected when visiting a route like: http://localhost:3000/cards/2?side=question

Hope this helps.

jason limmm
jason limmm
8,004 Points

when i tried to run the server it said "Cannot set headers after they are sent to the client"

here is all of my files so that you see if there is any problem outside the 3 i gave

app.js

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 mainroute=require('./routes');
const cardroutes=require('./routes/card/cards')

app.use(mainroute);
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');
});

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

cards.js

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

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

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

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

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

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

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

index.js

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

router.get('/', (req, res) => {
    const name = req.cookies.username;
    if (name) {
      res.render('index', { name });
    } else {
      res.redirect('/hello');
    }
});

router.get('/hello', (req, res) => {
  const name = req.cookies.username;
  if (name) {
    res.redirect('/');
  } else {
    res.render('hello');
  }
});

router.post('/hello', (req, res) => {
  res.cookie('username', req.body.username);
  res.redirect('/');
});

router.post('/goodbye', (req, res) => {
  res.clearCookie('username');
  res.redirect('/hello');
});

module.exports = router;

card.pug

extends layout.pug

block content
  section#content
    h2= text
    if hint
      p
        i Hint: #{hint}
    a(href=`${id}?side=${sideToShow}`)=sideToShowDisplay
    br
    a(href='/cards') Next card