异步调用,承诺挂起

aemubtdh  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(371)

我是node.js的新手,在身份验证时我运行代码promise always on pending。
错误

is Authenticated ?:  Promise { pending }

承诺{待定}
我尝试了不同的方法来使用等待,但我找不到正确的方法。谢谢你的帮助。
代码

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;
            }
        });
    });
};
egdjgwm8

egdjgwm81#

让我们重构第一个blog,将其命名为promisefunction。它可以用以下方式编写,以便它始终返回承诺。

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)
        });
});
}

以上代码将始终返回承诺。更好的方法是

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);
        }
    });
});
};

相关问题