Setting session before login throws `Error: Can’t set headers after they are sent`
See original GitHub issueI need to have sessionId
even before user logs in, to tracks browsing session of user and tie him to it later when he logs in, so I did this:
server.use(async (ctx, next) => {
if (!ctx.session.sessionId) {
ctx.session.sessionId = uuid.v4();
}
await next();
});
however if I do this I get (despite session id saving no problem)
{ Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:504:11)
at ServerResponse.setHeader (_http_outgoing.js:511:3)
at Cookies.set (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/cookies/index.js:115:13)
at ContextSession.save (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/lib/context.js:276:22)
at ContextSession.commit (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/lib/context.js:220:16)
at session (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/index.js:46:18)
at <anonymous> headerSent: true }
Any idea what I am doing wrong?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Cannot set headers after they are sent to the client in JS
The "Cannot set headers after they are sent to the client" error occurs when the server in an express.js application sends more than...
Read more >Cannot set headers after they are sent to the client | End Your If
Let's now go through the common scenarios causing Node.js and Express to throw error: can't set headers after they are sent.
Read more >"Can't set headers after they are sent to the client" error in ...
“Can't set headers after they are sent to the client” is one of the most common errors in Express.js. This error occurs when...
Read more >cannot set headers after they are sent to the client res.json
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function...
Read more >Cannot set headers after they are sent to the client node JS
Today we shall learn how to resolve HTTP Headers Sent Error. In the previous video, we have created login and register rest A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
there are lot’s of middlewares will not work with next.js if it still use
ctx.respond = false
, for now, you can’t set session in the same controller with next.js.already fixed in new koa version, it will ignore set header if body already sent.