+ 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.
6 Réponses
+ 1
Gordon Try this and see
https://code.sololearn.com/Wn0WL5FZzt7M/?ref=app
+ 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
+ 2
maybe for...of so you dont need to add [i] everywhere
+ 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.
0
So add break; in forEach?
Or I should use conventional for loop?
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.