JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Linking Around the Application

Shane Unger
Shane Unger
9,413 Points

Code is correct and works, but I'm getting an error in the console

I've implemented the code in this section, and when I go to a url with only a card number I'm redirected to the full url with the query string, but when that happens I get an error in my console Error: Can't set headers after they are sent, what's causing this and how do I fix it?

Shane Unger
Shane Unger
9,413 Points

oh and this is interesting, it happens as I'm ABOUT to change the url, before I've changed it and hit enter. I'll select the text in the url, hit delete, get ready to enter something else and the Error: Can't set headers after they are sent. pops up in my console

1 Answer

Shawn Rieger
Shawn Rieger
9,915 Points

I'm sure you've already moved onto the next video and in turn, figured this out. Just for other users looking, I'll put the fix here. res.redirect() doesn't stop the execution in the router.get() method, since your also calling res.render() at the bottom of the function, you get an error. So it's a simple fix, just use return...

return res.redirect(`/cards/${id}?side=question`);
Yuichi Narisawa
Yuichi Narisawa
19,548 Points

Thanks! Your answer fixes my error!

Thomas Couillard
Thomas Couillard
17,948 Points

You're a life saver Shawn, thanks a lot!

Kenneth Kim
Kenneth Kim
3,794 Points

This is a great help :)