0

I am trying to figure out how to pass res parameter (from post request) to the promise chain. I have following code:

router.post("/", (req, res) =>{
let email = req.body.username;
let pass = req.body.password;

conn.any("SELECT * FROM users WHERE email = $1", [email])
    .then((user)=>{
        if (user.length ===0){
            console.log("no user found");
            res.redirect("/");
        } else {
            console.log(res);
            bcrypt.compare(pass, user[0].password, (isMatched) =>{
                console.log(res);
                if (isMatched){
                    console.log("Horraaayyy!!!");
                    res.redirect("/");

                    return true;
                } else {
                    console.log("Nope!!!");
                    res.redirect("/");
                }

            })
        }
    })
    .catch(err =>{
        console.log("error: " + err )
        res.redirect("/");

});

});

After first statement .then, res becomes out of scope, and I cannot figure out how to res.render() after all .then statements

1 Answers1

0

In the code you posted res cannot be undefined unless express invokes your callback with an undefined value for res.

Here is your code properly aligned:

router.post(
  "/", 
  (req, res) => {
    //assuming express app and res is not undefined
    console.log("res is here:",res);
    let email = req.body.username;
    let pass = req.body.password;
    conn.any("SELECT * FROM users WHERE email = $1", [email])
    .then((user)=>{
      if (user.length ===0){
        console.log("no user found");
        res.redirect("/");
      } else {
        //impossible your res is undefined here
        console.log(res);
        bcrypt.compare(pass, user[0].password, (isMatched) =>{
            console.log(res);
            if (isMatched){
              console.log("Horraaayyy!!!");
              res.redirect("/");
              return true;
            } else {
              console.log("Nope!!!");
              res.redirect("/");
            }
        });
      }
    })
    .catch(err =>{
      console.log("error: " + err )
      res.redirect("/");
  });
});

your code with res not being undefined (res is in scope as closure):

var router = {
  post:(x,y)=>y(undefined,"hello world")
};
var bcrypt = {
  compare:(x,y,z)=>z(true)
}
var pass = 22;


router.post(
  "/", 
  (req, res) => {
    //assuming express app and res is not undefined
    console.log("res is here:",res);
    // let email = req.body.username;
    // let pass = req.body.password;
    // conn.any("SELECT * FROM users WHERE email = $1", [email])
    Promise.resolve([{password:12345}])
    .then((user)=>{
      if (user.length ===0){
        console.log("no user found");
        res.redirect("/");
      } else {
        //impossible your res is undefined here
        console.log("and res is now:",res);
        bcrypt.compare(pass, user[0].password, (isMatched) => {
            console.log("res in bcompare callback",res);
            // if (isMatched){
            //   console.log("Horraaayyy!!!");
            //   res.redirect("/");
            //   return true;
            // } else {
            //   console.log("Nope!!!");
            //   res.redirect("/");
            // }
        });
      }
    })
    .catch(err =>{
      console.log("error: " + err )
      // res.redirect("/");
  });
});

I suspect the code you posted is not the code giving you an undefined response. Maybe (I'm guessing here) your callback for bcrypt.compare is a seperate funciton outside the promise resolve handler (function in the then(resolveHandler))

If the compare callback is a seperate function you do need to pass res to it:

var router = {
  post:(x,y)=>y(undefined,"hello world")
};
var bcrypt = {
  compare:(x,y,z)=>z(true)
}
var pass = 22;
var compareCallback = 
  res => 
  isMatched => {
    console.log("res in bcompare callback",res);
    // if (isMatched){
    //   console.log("Horraaayyy!!!");
    //   res.redirect("/");
    //   return true;
    // } else {
    //   console.log("Nope!!!");
    //   res.redirect("/");
    // }
};

router.post(
  "/", 
  (req, res) => {
    //assuming express app and res is not undefined
    console.log("res is here:",res);
    // let email = req.body.username;
    // let pass = req.body.password;
    // conn.any("SELECT * FROM users WHERE email = $1", [email])
    Promise.resolve([{password:12345}])
    .then((user)=>{
      if (user.length ===0){
        console.log("no user found");
        res.redirect("/");
      } else {
        //impossible your res is undefined here
        console.log("and res is now:",res);
        bcrypt.compare(
          pass, 
          user[0].password, 
          compareCallback(res) // pass res to compareCallback and let it return a function
        );
      }
    })
    .catch(err =>{
      console.log("error: " + err )
      // res.redirect("/");
  });
});
HMR
  • 37,593
  • 24
  • 91
  • 160