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

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

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