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

576
Visualizações
Error [ERR_HTTP HEADERS_SENT]: Cannot set headers after they are sent to the client at nodejs

when i pass this url i need to get all user commit ---> http://localhost:3080/api/msgs/email=k1199@gmail.com but i getting one user commit only and getting this error [Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client]

app.get('/api/msgs/:msg', (req, res) => {
  console.log('api/users called!!!!!!!')
  const databuffer = fs.readFileSync('notes1.json', 'utf8')
  const datajson = databuffer.toString()
  const dtafil = JSON.parse(datajson);
  id = req.url.split('/')[3]
  findname = req.url.split('/')[2]
  console.log(findname)
  let a = id;
  let h = 0;
  let k = [];
  let pre = [];
  for (let i = 0; i < a.length; i++) {
    if (a[i] == "=") h = i;
  }
  for (let j = 1; j < a.length - h; j++) {
    k.push(a[h + j]);
  }
  for (let s = 0; s <= a.length - (k.length + 1); s++) {
    pre.push(a[s]);
  }
  var idegenerator = k.join("");  
  var idegeneratorlk = pre.join("")
  console.log(idegeneratorlk, idegenerator)
  if (idegeneratorlk == 'email=') {
    for(i=0;i<=dtafil.length;i++){
      if(dtafil[i].namees === idegenerator){
        console.log(dtafil[i])
        res.json(dtafil[i]);
      }
    }
    // const user = dtafil.find((i) => i.namees === idegenerator)
    // res.json(user);
  } else if (idegeneratorlk == 'youtube=') {
    const user = dtafil.find((i) => i.youtube === idegenerator)
    res.json(user);
  }
  else {
    res.json('404 Not fount');
  }
});

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In the simple term, ERR_HTTP_HEADERS_SENT means then you are sending a response more than one time for the same request.

As in you code see this for loop:

for(i=0;i<=dtafil.length;i++){
  if(dtafil[i].namees === idegenerator){
    console.log(dtafil[i])
    res.json(dtafil[i]);
  }
}

You are sending a JSON response in each iteration that will send the same response back to the client multiple times which you can't.

Save data in some variable and then send a response at once outside of foor loop something like this:

if (idegeneratorlk == 'email=') {
    const response = dtafil.filter(data => {
        return data.namees === idegenerator;
    })
    res.json(response);
}

I have just written this pseudo-code for reference which you may ned to edit as per your use case.

over 4 years ago · Santiago Trujillo 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