我想验证传入令牌,并尝试通过Id查找用户:
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
db.users.findById(decodeToken.id).then(function (foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}), function (error) {
return error;
}但我得到了“报答假”。我查看调试器的foundUser变量,它有消息
“参考错误: foundUser未定义”
在控制台中,我可以看到查询:
执行(默认):从“用户”中选择"id“、”用户名“、"email”、"password“、"createdAt”、"updatedAt“作为”用户“,”id“= 2;
我在数据库中有一个id=2的用户。为什么不起作用?
增添:
我尝试了MWY的修改过的例子:
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
findUserById().then(function(foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}), function (error) {
return error;
}
function findUserById() {
return db.users.findById(decodeToken.id).then(function (foundUser) {
return foundUser;
}), function (error) {
return error;
}
}
}并得到错误:
TypeError: findUserById(.).then不是函数
发布于 2017-04-03 02:33:40
一定要记住asynchronously
因为异步!您将首先得到false,然后得到结果!
这样您就可以像这样在ty.js文件中编写
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
return db.users.findById(decodeToken.id).then(function (foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}).catch(function (err) {
return err;
})
};在tu.js文件中:
var user = require('./ty');
user().then(function (result) {
//search findById result
console.log(result)
});https://stackoverflow.com/questions/43173383
复制相似问题