[英]pg-promise: passing res to promise
I am trying to figure out how to pass res parameter (from post request) to the promise chain. 我试图弄清楚如何将res参数(从后请求)传递到Promise链。 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 在第一个语句.then之后,res超出范围,而且我无法弄清楚在所有.then语句之后如何res.render()
In the code you posted res cannot be undefined unless express invokes your callback with an undefined value for res. 在您发布的代码中,res不能是不确定的,除非express用未定义的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 ): 您的代码中的res未被定义(res在范围内为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)
) 也许(我在这里猜测)您对
bcrypt.compare
的回调是一个在promise resolve处理程序之外的单独函数( then(resolveHandler)
中的函数)
If the compare callback is a seperate function you do need to pass res to it: 如果compare回调是一个单独的函数,则需要将res传递给它:
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("/"); }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.