简体   繁体   English

未处理的拒绝-护照登录应用程序节点js

[英]Unhandled Rejection - passport login app node js

I'm creating nodejs login app and using passport js for authentication. 我正在创建nodejs登录应用程序,并使用passport js进行身份验证。 I use a local mysql database and use sequelize as ORM library. 我使用本地mysql数据库,并使用sequelize作为ORM库。

In my user model, I have created to get the user by username. 在我的用户模型中,我创建了按用户名获取用户的方法。

module.exports.findUserByUsername = function(username, callback){
  User.findOne({ where: {username: username} }).then(function(user) {
    var userObj = user.get({plain:true});
    callback(userObj);
  })
}; 

In router file 在路由器文件中

passport.use('login', new LocalStrategy(
    function (username, password, done) {

        User.findUserByUsername(username, function (err, user) {
            if(err) {
                throw err;
            }
            console.log(user);
        })
    }
));

I'm getting an error Unhandled rejection (<{object_values...>, no stack trace) 我收到错误Unhandled rejection (<{object_values...>, no stack trace)

How can I solve this issue? 我该如何解决这个问题?

You need to change callback calling like this : 您需要像这样更改回调调用:

callback(null, userObj);

In router file you throw when encounter to error. 在路由器文件中,遇到错误时会throw该错误。 Throwing exception in async calling environment is not a good practice. 在异步调用环境中引发异常不是一个好习惯。 Anyway you call findUserByUsername passing a callback expecting first argument is error. 无论如何,您调用传递一个期望第一个参数的回调的findUserByUsername都是错误的。

In findUserByUsername you return user object in error place. findUserByUsername ,将用户对象返回错误的位置。 So your logic in router file thinks it's an error. 因此,您在路由器文件中的逻辑认为这是一个错误。

For avoiding such these exceptions you need not to throwing exception, return them in callback functions. 为避免此类异常,您无需抛出异常,请在回调函数中将其返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM