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
Laurence kite
11,768 Pointsexpress error message: Can't set headers after they have been sent
I am following express basics by Andrew Chalker and all was going well until adding the the card.js file for random cards and typing a number to get a card..
Although the app still runs and the card is displayed on the page as expected I get an error in the terminal program every time a certain route is accessed that has a res.redirect in it.
I thought it was me and probably is, but I download the code and paste it in for all Javascript files but the error is not going away.. I am not sure if its because he is using an older version of express or what it is..
const express = require('express');
const router = express.Router();
const { data } = require('../data/flashCardsData.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;
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;
Ben Newton
14,726 PointsBen Newton
14,726 PointsI see this error too, I'm at the point where I'm adding the 'next card' link. Everything actually works fine, but this error presents every-time we go through the route of '/cards/' which inturn redirect to '/cards/:id'