![](/img/trans.png)
[英]TypeError: Cannot read property 'headersSent' of undefined when using Passport.js
[英]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);
}
我想到了兩種方法來解決這個問題:
將 ctrlAuth 綁定到方法:
router.post('/login', ctrlAuth.login.bind(ctrlAuth));
創建另一個類並將其用作下一個回調:
router.post('/login', ctrlAuth.login, ctrlAuth.processUser);
在 ctrAuth.login 中,我調用next();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.