Closed jjdechavez closed 2 years ago
https://github.com/jjdechavez/portfolio/blob/feature/project-form/src/pages/api/login.ts#L21-L26
Consider when isAuth.login == false
, both res.status(500).send(isAuth)
and res.send(isAuth)
is called, so that is why Error [ERR_STREAM_WRITE_AFTER_END]: write after end
you are sending twice
This is not related to this library btw, but try it out and see if it helps.
Thank you for the response and I'm sorry if it's not library issue. Sorry for my logic error on my code. My I know whats the better approach to handle this kind of error.
I just updated the condition for isAuth.login == false
if (!isAuth.login) {
res.status(500).send(isAuth);
return;
}
It still throws the same error.
It's ok for me to close this issue.
I saw same issue that I'm having right now, Tells that I should only use one res
. After, trying the resolve issue.
if (!isAuth.login) {
return;
}
It still throws the same error.
Having the same error.
import nc from "next-connect";
import { getSession } from "next-auth/client";
const handler = nc();
handler.put(async (req, res) => {
const session = await getSession({ req });
if (!session) {
res.status(403).end();
}
if (!req.files) {
res.status(400).end();
}
console.log('Request looks fine!');
// Some logic here...
res.end();
});
export default handler;
Anytime the flow goes into res.code().end()
it throws ERR_STREAM_WRITE_AFTER_END
.
If I use return
it seems to work fine.
if (!session) {
return res.status(403).end();
}
if (!req.files) {
return res.status(400).end();
}
I am suspecting that the session libraries are doing something with the response (such as automatically assign header on response sent), which accidentally cause this. I might have to look more into it though.
Can you try instead of res.status()
or res.send()
do:
res.statusCode = code;
res.end()
...
@hoangvvo after checking the next-iron-session library, saw that in every call withIronSession method. It set the response header. You can check here: https://github.com/vvo/next-iron-session/blob/8280c18ac3ecb7314ecc531bf624e3cbfac211a1/lib/index.js#L72
Your right the issue came from into the session library.
Good day everyone, I'm having issue when running my next-app it always keep throwing when I'm working with the api
I already tried to remove
node_modules
yarn.lock
and update the npm. Stills throw the same error.I just moved into Manjaro KDE 21.0.5 node v14.17.0 npm 7.14.0 next-connect ^0.10.1
You can check my repo to replicate it https://github.com/jjdechavez/portfolio/tree/feature/project-form After cloning, make sure to setup the env.local
Sample of env.local
Then run
yarn dev
ornpm run dev
Go to the
/login
page then it will throw the errorI hope someone could help me! TIA