question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting session before login throws `Error: Can’t set headers after they are sent`

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
dead-horsecommented, Jul 28, 2017

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.

3reactions
dead-horsecommented, Apr 8, 2018

already fixed in new koa version, it will ignore set header if body already sent.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found