Home > other >  Program works but I keep getting "Cannot set headers after they are sent to the client", s
Program works but I keep getting "Cannot set headers after they are sent to the client", s

Time:09-04

I don't know if this is enough code to post, but this is the line that is causing my the error: (The res.redirect) line

app.post("/home/finishform",urlencodedParser, (req,res)=>{

  const score = new Score(req.body)

  score.save()
    .then((result)=>{
      res.redirect("/")
    }).catch((err)=>{
      console.log(err)
    })
  res.redirect("/home/FinishPage/client")// This is causing the error, but it's actually redirecting, so it's working but I keep getting an error. 
})

CodePudding user response:

You have two calls to redirect, which causes the error. You should probably only redirect if the saving was successful:

app.post("/home/finishform",urlencodedParser, (req,res)=>{

  const score = new Score(req.body)

  score.save()
    .then((result)=>{
      res.redirect("/home/FinishPage/client") // Here!
    }).catch((err)=>{
      console.log(err)
    })
})
  • Related