簡體   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