Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda