简体   繁体   English

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

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

I am calling the following from one class:我从一个班级调用以下内容:

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

Then the ctrlAuth is from another class:然后 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) {...}

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

So far, this is how I did it, but still doesn't work:到目前为止,这就是我所做的,但仍然不起作用:

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

There are two ways I thought of fixing this:我想到了两种方法来解决这个问题:

  1. Bind the ctrlAuth to the method:将 ctrlAuth 绑定到方法:

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

  2. Create another class and use it as next callback:创建另一个类并将其用作下一个回调:

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

    Inside ctrAuth.login, I call on next();在 ctrAuth.login 中,我调用next();

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

相关问题 TypeError:使用Passport.js时无法读取未定义的属性“ headersSent” - TypeError: Cannot read property 'headersSent' of undefined when using Passport.js 需要通行证之后,“ TypeError:无法读取未定义的属性'isAuthenticated'” - 'TypeError: Cannot read property 'isAuthenticated' of undefined' after requiring passport.js Passport.js:无法读取未定义的属性“用户名”(节点) - Passport.js: Cannot read property 'username' of undefined (Node) Passport.js github策略回调总是返回“TypeError:无法设置未定义的属性'用户'” - Passport.js github strategy callback always returns “TypeError: Cannot set property 'user' of undefined” 类型错误:无法读取未定义 nodejs 的属性“passport” - TypeError: Cannot read property 'passport' of undefined nodejs 类型错误:无法读取未定义的属性“护照” - typeerror: cannot read property 'passport' of undefined Passport.js成功认证完全不调用 - Passport.js successful authentication not calling at all 如果字段为空,Passport.js不会调用LocalStrategy - Passport.js not calling LocalStrategy if fields are empty passport.js成功验证没有调用next() - passport.js successful authentication not calling next() Passport.js“类型错误:passport.serialize不是函数” - “TypeError: passport.serialize is not a function” with Passport.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM