gpt4 book ai didi

mysql - 异步调用, promise 待处理

转载 作者:行者123 更新时间:2023-11-29 16:28:07 25 4
gpt4 key购买 nike

我是 Node.js 新手,在身份验证中运行代码时 Promise 始终处于挂起状态。

错误

is Authenticated ?:  Promise { pending }

promise {待处理}

我尝试了不同的方法来使用等待,但找不到正确的方法。感谢帮助。

代码

return new Promise((resolve, reject) =>
{
console.log(email, password);
var isAuthenticated = false;
isAuthenticated= model.authenticate(email, password).then(function(result){
//console.log(model.authenticate(email, password));
console.log("is Authenticated ?: ",isAuthenticated);
if(isAuthenticated) {
console.log(isAuthenticated);
console.log("Success!");
res.render('/home');
} else {
console.log("ko");
res.redirect("/");
}
}).catch(function(error) {
console.log(error);
});
});




User.prototype.authenticate = function( email, password ) {
connection = this.connection;
return new Promise(function(resolve, reject){
connection.query('SELECT * FROM users WHERE email = ? AND Password = ? AND is_deleted = 0',[email, password],
function (error, results, fields) {
if (error){
console.log("error ocurred",error);
reject(error);
return false;
} else {
resolve(results);
return true;
}
});
});
};

最佳答案

让我们重构第一个博客,将其命名为 promiseFunction。它可以用这些方式编写,以便它始终返回 promise 。

 const promiseFunction = function (req, res, next) {
// I am assuming it is a express middleware and res is passed as the function arguments.
return new Promise((resolve, reject) => {
console.log(email, password);
var isAuthenticated = false;
isAuthenticated = model.authenticate(email, password)
.then(function (result) {
//console.log(model.authenticate(email, password));
console.log("is Authenticated ?: ", isAuthenticated);
if (isAuthenticated) {
console.log(isAuthenticated);
console.log("Success!");
res.render('/home');
return resolve()
}
else {
console.log("ko");
res.redirect("/");
return resolve()
}
}).catch(function (error) {
console.log(error);
return reject(error)
});
});
}

上面的代码将始终返回 promise 。更好的方法是

const promiseFunction = function (req, res, next) {
// I am assuming it is a express middleware and res is passed as the function arguments.
return model.authenticate(email, password)
.then(function ({ isAuthenticated }) {
//console.log(model.authenticate(email, password));
console.log("is Authenticated ?: ", isAuthenticated);
if (isAuthenticated) {
console.log(isAuthenticated);
console.log("Success!");
res.render('/home');
}
else {
console.log("ko");
res.redirect("/");
}
})
}

验证函数将是:

User.prototype.authenticate = function (email, password) {
connection = this.connection;

return new Promise(function (resolve, reject) {
connection.query('SELECT * FROM users WHERE email = ? AND Password = ? AND is_deleted = 0', [email, password], function (error, results, fields) {
if (error) {
console.log("error ocurred", error);
return reject(error);
}
else {
var newResponse = Object.assign({}, response, {isAuthenticated: true})
return resolve(newResponse);
}
});
});
};

关于mysql - 异步调用, promise 待处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54123001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com