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

179
Vistas
Cannot set headers after they are sent to the client express node

This is the post request I tried to create for login

router.post('/login', async(req, res) =>{

const user =  await User.findOne({gmail: req.body.gmail})
!user && res.status(404).json("user not matched")

const p = await bcrypt.compare(req.body.password, user.password)
!p && res.status(400).json("password not matched") 

res.status(200).json(user)
}   

)

When I click the send button in postman I get this response with the following error: enter image description here

This is the error I get when I "send" a post request in postman:

::1 - - [19/Jan/2022:11:48:06 +0000] "POST /api/auth/login HTTP/1.1" 200 22 node:internal/errors:464 ErrorCaptureStackTrace(err); ^

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 ServerResponse.header (D:\Projects\social\node_modules\express\lib\response.js:776:10) at ServerResponse.send (D:\Projects\social\node_modules\express\lib\response.js:170:12) at ServerResponse.json (D:\Projects\social\node_modules\express\lib\response.js:267:15) at D:\Projects\social\routes\auth.js:28:21 { code: 'ERR_HTTP_HEADERS_SENT' } [nodemon] app crashed - waiting for file changes before starting...

after I click the "send" button second time, the response shows this enter image description here

what did I do wrong here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is a very common error and is spread across the internet. You're trying to send 2 responses. When your check on the line 5 succeeds - you send a response, then your code continues and reaches the line 7 where you're sending a response as well. Just wrap them inside an if-else block to prevent multiple responses.

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