/* node solved.js */ ERR_HTTP_HEADERS_SENT | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

/* node solved.js */ ERR_HTTP_HEADERS_SENT

I got this error when I tried to visit any posts other than the first post I created: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:482:11) Here are my codes: app.js https://code.sololearn.com/W7RU5635SIin/ post.ejs https://code.sololearn.com/W2GxyA6oP8WF/ compose.ejs https://code.sololearn.com/WD2gYz0LcJQj/ I tried Googling it, and some stackoverflow answers say to remove one res.send(), problem is ... I am not using res.send() at all, all is res.render with EJS, and in app.get("/posts/:postTitle",... I have the if-else. Appreciate a solution. Thanks in advance.

10th Feb 2020, 2:46 PM
Gordon
Gordon - avatar
6 Answers
11th Feb 2020, 1:58 PM
Calviղ
Calviղ - avatar
+ 3
end the loop after sending a response/render. because i think the callback function doesnt end after render()/send() when the loop continued, it'll try to render and send other post in the array after the original response was sent
10th Feb 2020, 2:57 PM
Taste
Taste - avatar
+ 2
maybe for...of so you dont need to add [i] everywhere
10th Feb 2020, 3:12 PM
Taste
Taste - avatar
+ 1
foreach would continue to iterate for all post of posts array. It would create multiple renders in a get request posts, but get request only allow to call render or redirect once. Use for loop with break to ensure it only render/redirect once. Don't use foreach, since we can't exit foreach loop with break.
11th Feb 2020, 12:50 PM
Calviղ
Calviղ - avatar
0
So add break; in forEach? Or I should use conventional for loop?
10th Feb 2020, 3:11 PM
Gordon
Gordon - avatar
0
👌 Thanks for your answer. I already shut down my computer and prepared to sleep for now. I'll test tomorrow and let you know ~ Thanks again, Taste.
10th Feb 2020, 3:21 PM
Gordon
Gordon - avatar