繁体   English   中英

调用this.somemethod的passport.js抛出TypeError:无法读取属性'somemethod'

[英]passport.js calling this.somemethod throws TypeError: Cannot read property 'somemethod'

我从一个班级调用以下内容:

router.post('/login', ctrlAuth.login);

然后 ctrlAuth 来自另一个类:

login(req, res) {
    passport.authenticate('ldapauth', function (err, user, info) {
        ... // handling err and !user
        ...
        if (user) this.processUser(req, res);
    })(req, res);
}

private processUser(req, res) {...}

然后我得到 TypeError: Cannot read property 'processUser' of undefined

到目前为止,这就是我所做的,但仍然不起作用:

login(req, res) {
    const callback = ((err, user, info) => {
        if (user) this.processUser(req, res);
    }).bind(this);
    passport.authenticate('ldapauth', {session: false}, callback)(req, res);
}

我想到了两种方法来解决这个问题:

  1. 将 ctrlAuth 绑定到方法:

    router.post('/login', ctrlAuth.login.bind(ctrlAuth));

  2. 创建另一个类并将其用作下一个回调:

    router.post('/login', ctrlAuth.login, ctrlAuth.processUser);

    在 ctrAuth.login 中,我调用next();

暂无
暂无

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

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