Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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');
  }
});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!