如何基于异步函数的结果对Express响应对象进行操作?

时间:2019-03-09 20:16:01

标签: javascript mysql node.js express

我正在尝试创建一个Express应用程序的简单组件,该组件通过GET请求的uri参数获取登录凭据,并根据所提供的凭据是否与MySQL凭据匹配来为用户登录页面或错误页面提供服务。

据我所知,我需要将渲染功能作为回调提供给凭据AreValid(),将散列比较作为MySQL查询的回调提供...不幸的是,我在为凭据AreValid()的内部苦苦挣扎。就目前而言,我收到“ TypeError:无法读取未定义的属性'res'”,我认为这是因为凭据AreValid()在readOnlyConnection.query()调用回调之前已解析?

因为readOnlyConnection.query()是MySQL的一个函数,所以我不知道如何从凭据AreValid()中传入res参数。我到JS / Express大约要花两周的时间,所以我可能无法正确完成整个任务。

如果有人可以告诉我,尽管将两者都强制转换为String且看起来相同,为什么比赛返回false,则奖励积分。 (MySQL哈希的类型为BLOB,但似乎可以正常使用,即使使用==运算符也失败)

相关代码:

    router.get('/login/:username/:hash', function(req, res, next) {
            console.log(`attempted login with username=${req.params.username} and hash=${req.params.hash}`);
            login.credentialsAreValid(req.params.username, req.params.hash, res, function(res, valid) {
                    if (valid) {
                            console.log("Login Successful").
                            res.render('login', { title: 'success' });
                    }
                    else {
                            console.log("Login Unsuccessful").
                            res.render('login', { title: 'fail' });
                    }
            })
    });


    credentialsAreValid = function (username, password, res, callback) {
            console.log(`Checking password for user "${username}"`);
            var clientHash = String(createHash(password));
            console.log(`Hashed ${password} to ${clientHash}`);
            var hashesMatch = readOnlyConnection.query(`SELECT passwordHash FROM users WHERE users.username = "${username}";`, function(err, rows, fields) {
                    var serverHash = (rows.length > 0) ? String(rows[0].passwordHash) : "";
                    console.log(`Comparing clientHash ${clientHash} with serverHash ${serverHash} (${clientHash == serverHash})`);
                    return callback(res, clientHash === serverHash);
                    console.log(`This should never display`);
         });
    };

服务器控制台输出:

attempted login with username=groucho and hash=password
Checking password for user "groucho"
Hashed password to password
Comparing clientHash password with serverHash password (false)
Login Unsuccessful
/home/node/alex-practice/node_modules/mysql/lib/protocol/Parser.js:80
                throw err; // Rethrow non-MySQL errors
                ^

TypeError: Cannot read property 'res' of undefined
        at /home/node/alex-practice/routes/index.js:43:46
        at Query.<anonymous> (/home/node/alex-practice/login-helper.js:33:16)
        at Query.<anonymous> (/home/node/alex-practice/node_modules/mysql/lib/Connection.js:502:10)
        at Query._callback (/home/node/alex-practice/node_modules/mysql/lib/Connection.js:468:16)
        at Query.Sequence.end (/home/node/alex-practice/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
        at Query._handleFinalResultPacket (/home/node/alex-practice/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
        at Query.EofPacket (/home/node/alex-practice/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
        at Protocol._parsePacket (/home/node/alex-practice/node_modules/mysql/lib/protocol/Protocol.js:278:23)
        at Parser.write (/home/node/alex-practice/node_modules/mysql/lib/protocol/Parser.js:76:12)
        at Protocol.write (/home/node/alex-practice/node_modules/mysql/lib/protocol/Protocol.js:38:16)
[nodemon] app crashed - waiting for file changes before starting...

0 个答案:

没有答案