Home > front end >  I'm trying to run this code but it is still sending me the error can't not set headers aft
I'm trying to run this code but it is still sending me the error can't not set headers aft

Time:02-17

router.post("/check", async (req, res) => {
    const nom = req.body.nom
    const postnom = req.body.postnom
    const matricule = req.body.matricule
    const numeroBordero = req.body.numero_bordero
    const paymentOrder = req.body.payementOrder

    const etudiant = Etudiant.findOne({ matricule }, function (err, result) {
        if (result == null) {
            req.flash("test", "veuillez entrer des infos correctes") 
            res.redirect("/");
        } else if (result !== null) {
            if (result.isRegistered !== true) {
                Etudiant.updateOne({
                    _id: result._id
                }, {
                    isRegistered: true
                }, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log("correctly updated");
                    }
                })
            }
        }
    }) 
    
    return res.render("profil", {
        etudiant
    });
})

CodePudding user response:

Once you call redirect, you cannot call render.

Return after your redirect.

res.redirect("/");
return

CodePudding user response:

What front end framework are you using and what are you trying to accomplish with this endpoint? It seems like the function is checking the value of the request for a record, the running an update if a record was found.

If you want the render on the ui to be conditional on the result of this api call, you need to render separate html accordingly.

I am used to using react or angular, where I simply send a json object to the UI, and let the ui determine which page to navigate to, or which component to conditionally render.

CodePudding user response:

use return res.redirect("/"); instead

CodePudding user response:

You want use async/await but only async is present in your code.

Next, use result === null instead of result == null. I didn't test your code, but can you run this code ? I think it'll solve your issue.

This code use promises.

router.post("/check", (req, res) => {
  const nom = req.body.nom
  const postnom = req.body.postnom
  const matricule = req.body.matricule
  const numeroBordero = req.body.numero_bordero
  const paymentOrder = req.body.payementOrder

  Etudiant.findOne({ matricule }, function (err, result) {
    if (result === null) {
      req.flash("test", "veuillez entrer des infos correctes") 
      res.redirect("/");
    } else if (result !== null) {
      if (result.isRegistered !== true) {
        Etudiant
          .updateOne(
            {_id: result._id},
            { isRegistered: true}
          )
          .then(() => {
            console.log("correctly updated")
          })
          .catch((err) => {
            console.log(err)
          })
      }
  })
})
  • Related