Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ismayil Aliyev
18,560 PointsError: Cannot set headers after they are sent to client (for redirecting when there is no side variable)
In the redirecting challenge, I wrote code exact as in video and it works. But command line gives me such kind of message:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Please, help me to understand why I got such a message even if app works properly, and how can I solve it or should I?
2 Answers

Ismayil Aliyev
18,560 PointsDear friends, I could figure out why cmd was giving me such kind of error.
router.get('/:id', (req, res) => {
const { side } = req.query;
const { id } = req.params;
if(!side){
return res.redirect(`/cards/${id}?side=question`)
}
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);
});
The main reason was router.get() method couldn't finish its work because of redirecting. I found solution in writing return value before redirecting. Thanks a lot for your attention :) .

Victoria Medley
17,085 PointsWhat do you mean by this? Where did you put the return value and what did you write exactly? Could you post a snippet of the completed code? I am stuck on the same issue, I tried what you said but cannot figure it out.

Gabbie Metheny
33,777 PointsAndrew addresses this a couple videos later, here. Basically, the rest of the router.get()
function tries to execute, so we're trying to redirect and render simultaneously. Attempting to render after the redirect is what's causing the console error. Using the return
keyword breaks us out of the router.get()
function when we redirect, before the res.render()
.

Finley Williamson
11,375 PointsThank you very much!

Jaime Jesus Guerra Torres
16,953 PointsI solved it, encasing the rest of the code in a "else" statement
router.get('/:id', (req, res) => {
const { side } = req.query;
const { id } = req.params;
if(!side) {
res.redirect(`/cards/${id}?side=question`);
} else {
const text = cards[id][side];
const { hint } = cards[id];
const templateData = { text, id };
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);
}
});

Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,004 PointsThis solved the problem for me.
This is because the router was trying to render after a redirect?
Michael Hulet
47,842 PointsMichael Hulet
47,842 PointsYou've called
res.set
(or a related method) sometime afterresponse.send
, which isn't allowed. We'd need to know more about your code to point to a specific line