我创建了一个函数来获取用户 email & password
,然后在数据库中运行几次检查以查看详细信息是否有效。如果详细信息有效,则用户将得到一个 API
key 的解析。
但出于某种原因,一些函数在其他函数之前被调用,我在一个应该返回 userObj
的变量上得到 undefined
。
在该函数中,我添加了控制台日志 0、1、2、4 和 5,因此您可以看到我在调用该函数时得到的奇怪输出。我将在下面包含我的代码和控制台输出!我正在为我的模型使用 sequalize
,我知道它们会返回正确的数据,因为我已经对其进行了测试。我也在使用 promises
。
代码:
var auth = {
login: function(req, res) {
var email = req.body.email || '';
var password = req.body.password || '';
// If nothing parsed, return json obj
if (email == '' || password == '') {
console.log("1");
res.status(401);
res.json({
"status": 401,
"message": "Invalid credentials"
});
return;
}
// Fire a query to your DB and check if the credentials are valid
var dbUserObj = auth.validateUser(email, password);
console.log("0 _____");
console.log(dbUserObj); // I am returning undefined??? Why the!
if (!dbUserObj) { // If authentication fails, we send a 401 back
res.status(401);
res.json({
"status": 401,
"message": "Invalid credentials"
});
return;
}
console.log("1 ____");
authToken.create({ userId: dbUserObj.userId, token: genToken(dbUserObj)}).then(function(results){
res.json("token", results.token);
});
console.log("2 ____");
},
validateUser: function(email, password) {
// console.log(email + " pass: " + password);
console.log("3 ____");
user.findAll({
where: {
email : email,
password : password
},
raw: true
}).then(function(results) {
console.log("4 _____");
console.log(results);
return results;
// results prints out with all the correct values!!!!!!!!!!
}).catch(function (err) {
return err;
console.log(err);
});
console.log("5 _____");
},
控制台输出:
你可以看到我在 dbUserObj
对象上得到了未定义,但老实说我不知道为什么,因为我正在打印结果 BEFORE
我'我实际上正在归还它们(如下所示)。
3 ____
5 _____
0 _____
undefined
POST /login/ 401 1.341 ms - 46
Executing (default): SELECT `userId`, `name`, `password`, `email`, `authKey`, `profilePic`, `coverPic`, `description`, `createdAt`, `updatedAt` FROM `user` AS `user` WHERE `user`.`email` = 'jameswain10@gmail.com' AND `user`.`password` = 'f6588fc86e19db0936d9b7e8fd3d6c6544af6cdcc3ce161da88adcdb4b952adb';
4 _____ RESULTS ARE BELOW ----
[ { userId: 1,
name: 'James',
password: 'f6588fc86e19db0936d9b7e8fd3d6c6544af6cdcc3ce161da88adcdb4b952adb',
email: 'james@gmail.com',
authKey: '$$$Ddjjdafjjadsfjadsjfjadfsj2',
profilePic: 'default.png',
coverPic: 'default.png',
description: 'This user doesn\'t have a description!',
createdAt: Wed Dec 23 2015 12:48:35 GMT+1100 (AEDT),
updatedAt: Wed Dec 23 2015 12:48:35 GMT+1100 (AEDT) } ]
正如您在上面的控制台输出
中看到的,某些日志在注销之前和之后应该是?有谁知道为什么会这样!?
最佳答案
您遇到了异步问题。
让验证用户返回 promise :
validateUser: function(email, password) {
// console.log(email + " pass: " + password);
console.log("3 ____");
return user.findAll({
where: {
email : email,
password : password
},
raw: true
});
在您的登录函数中,您可以然后
该函数的结果。
login: function(req, res) {
var email = req.body.email || '';
var password = req.body.password || '';
// If nothing parsed, return json obj
if (email == '' || password == '') {
console.log("1");
res.status(401);
res.json({
"status": 401,
"message": "Invalid credentials"
});
return;
}
// Fire a query to your DB and check if the credentials are valid
auth.validateUser(email, password)
.catch(function() {
//.. handle error
}
.then(function(results) {
var dbUserObj = results;
console.log("0 _____");
console.log(dbUserObj); // I am returning undefined??? Why the!
if (!dbUserObj) { // If authentication fails, we send a 401 back
res.status(401);
res.json({
"status": 401,
"message": "Invalid credentials"
});
return;
}
console.log("1 ____");
authToken.create({ userId: dbUserObj.userId, token: genToken(dbUserObj)}).then(function(results){
res.json("token", results.token);
});
console.log("2 ____");
}
},
奖励答案:
你变得不确定,因为这里的这一行:
then(function(results) {
console.log("4 _____");
console.log(results);
return results;
// results prints out with all the correct values!!!!!!!!!!
这是将结果返回给“then”函数,该函数正在创建另一个用您的 dbobject 解决的 promise ,但是您没有将它返回给 top 函数。在不返回 promise 或调用回调的函数中使用 promise 是一个很好的危险信号,表明您做错了什么。
关于javascript - 在 Node 中验证用户时得到奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427367/