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 Express Basics Parameters, Query Strings, and Modularizing Routes Adding Links

Finley Williamson
Finley Williamson
11,375 Points

I know this doesn't work.. but why??

// running this code throws an error:
//Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

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

  if (side === "answer") {
    const {hint} = cards[id];  // here is where I think it's going wrong
  }

  const templateData = {text,hint};

  res.render('card', templateData);
});

If I understand correctly, because hint is being declared in a conditional, it throws an error when I try to reference it in the declaration of templateData.

However, my brain tells me hint should default to undefined, and run correctly.

Why then does the app throw this error?

Thank you in advance :D

1 Answer

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

Hi Finley,

You are correct about the hint declaration causing an error. Since it is only defined within an if statement, then it only exists within that statement. Anything that tries to call hint from the outside will get an undeclared variable error which will crash JavaScript.

You need to remove this line from the conditional and operate the conditional another way.