Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
code: 'ERR_HTTP_HEADERS_SENT' } error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Am trying to send a request to the server to save a post. Am using Nextjs and everytime i try to send it gives me this Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

Here is my code

Post.js
async function uploadPost(post) {

const response = await fetch('/api/savePost', {
  method: 'POST',
  body: post,
});

if(!response.ok) {
  throw new Error('Failed to post')
}

return await response.json();

}

savePost.js

export const config = {
      api: {
      bodyParser: false,
   },
}

export default const post = async (req, res) => {
  if (req.method !== 'POST') {
    return res.status(405).json({
     message: 'Method not allowed'
    })
  }

  // const postData = JSON.parse(req.body);
  const savedPost = await prisma.post.create({
   data: req.body
  });

  res.status(200).json({message: 'Post saved successfully'})
  res.json(savedPost)
 };

Am using Prisma for the db I get this error

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at NodeNextResponse.setHeader (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-http\node.js:56:19)
at DevServer.renderError (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-server.js:1134:17)
at DevServer.renderError (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\next-server.js:493:22)
at DevServer.run (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\dev\next-dev-server.js:452:35)
at async DevServer.handleRequest (C:\Users\user\Documents\My Received Files\js\twitter-clone\node_modules\next\dist\server\base-server.js:307:20) {
 code: 'ERR_HTTP_HEADERS_SENT'
}
error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  res.status(200).json({message: 'Post saved successfully'})  
  res.json(savedPost)

Here you send 2 messages back. Both with 200 code. This is what "write after send" means.

The first message set's the header and goes back.

Then after that you try to do it again, but the header is already sent.

// res.json() equals to res.status(200).json()

// You can merge res.status(200).json({message: 'Post saved successfully', savedPost: savedPost})

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda