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.

JavaScript Express Basics Serving Static Files in Express Adding Static Assets to the App

Othneil Drew
Othneil Drew
22,421 Points

Too many redirects for handling error where side doesn't equal 'question' or 'answer'?

Hi, I'm trying to handle the error where if the side doesn't equal 'question' or 'answer' the application automatically redirects to show the question. However, I get an error that the server had too many redirects.

Here is my code below that results in the error:

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

What is the correct action to avoid this?

3 Answers

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,756 Points

I don't know why none of the other commenters caught this, but this conditional:

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

will always trigger causing a loop. The or statement makes sure of that, because side only needs to not be equal to question OR not be equal to answer. Meaning that even if it is equal to question or answer, it will not be equal to the other, and therefore the condition will be triggered as true. This could be simply solved by changing the logical or operator to a logical and operator as so:

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

You're likely getting caught in an infinite loop. You should add a default condition in the event that side doesn't equal "question" or "answer". So it would be something like:

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

I would add "console.log(side);" to the else condition so you can see what the value of "side" is.

Othneil Drew
Othneil Drew
22,421 Points

Thanks for answering Scott. However, I didn't get any success with this method.

I think there is an alternative solution without any if statement or redirection.

const side = req.query.side || 'question';

Here I assign the string 'question' if the side property of the query string is undefined.