2
votes

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:535:11) at ServerResponse.header (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\express\lib\response.js:267:15) at ServerResponse.send (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\express\lib\response.js:158:21) at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\API\Routes\Signuproutes.js:109:13 at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\model.js:4876:16 at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16 at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\model.js:4899:21 at model. (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\model.js:495:7) at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\kareem\index.js:315:21 at next (C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\kareem\index.js:209:27) at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\kareem\index.js:182:9 at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\kareem\index.js:507:38 at processTicksAndRejections (internal/process/task_queues.js:79:11) Emitted 'error' event on Function instance at: at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\model.js:4878:13 at C:\Users\Abubakar\Desktop\Desktop Docs\Full Stack Web Devop Data\FPLatest\FinalFullstack\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16 [... lines matching original stack trace ...] at processTicksAndRejections (internal/process/task_queues.js:79:11) { code: 'ERR_HTTP_HEADERS_SENT' }

   
  router.post('/Users',(req,res)=>{
    let newuser=new user( {

      name:req.body.name,
      email:req.body.email,
      password:req.body.password,
  });

    if(!req.body.name || !req.body.email || !req.body.password){

      res.json("Please fill the Credentials")
      return}
    user.findOne({name:req.body.name},(err,User)=>{

      if(User)
      {

          res.json("User Name Already Registered")
          return
      }

    })
  user.findOne({email:req.body.email},(err,User)=>{
    if(User)
    {
      res.send({msg:"Email Already Registered"})
      return
    }
  })
  newuser.save((err,user)=>
  {
      if (err) throw err
      else
      {
        res.send({msg:' User added in Database'})
      }
  })
  });
1

1 Answers

1
votes

This is happening because your code is calling res.send several times. Check out this question.

user.findOne({name:req.body.name},(err,User)=>{

  if(User)
  {

      res.json("User Name Already Registered")
      return //////////////// THIS RETURN STATEMENT DOES NOT EXIT THE MAIN FUNCTION
  }

})

You should do something like this:

user.findOne({name:req.body.name},(err,User)=>{

  if(User)
  {

      res.json("User Name Already Registered")
      return
  }

  user.findOne({email:req.body.email},(err,User)=>{
      if(User)
      {
          res.send({msg:"Email Already Registered"})
          return
      }

      newuser.save((err,user)=>
      {
          if (err) throw err
          else
          {
              res.send({msg:' User added in Database'})
          }
      })
     });
  })


})