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

187
Visualizações
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 Respostas
Responde à pergunta

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