<分区>
我正在为我的后端服务使用 nodejs + Express。
我有一个 authenHandler.js 文件来帮助使用 sequelize 进行身份验证:
module.exports = {
isAuthenticated: function(data) {
models.Users.find(data)
.then(function(user) {
if (user) {
return true;
} else {
return false;
}
});
}
}
当我在 app.js 中使用这个辅助函数时:
app.use(function(req, res, next) {
// process to retrieve data
var isAuthenticated = authProvider.isAuthenticated(data);
console.log(isAuthenticated);
if (isAuthenticated) {
console.log("auth passed.");
next();
} else {
var err = new Error(authenticationException);
err.status = 403;
next(err);
}
}
})
这总是转到 else 语句,因为 isAuthenticated 打印行总是返回未定义。看起来 promise 在调用 if-else 语句后返回了值。
我不确定如何连接 authenHandler.js 和 app.js。最好的方法是什么?