1
私は、関数からクラスへコードを書き直すことにしました。しかし、私はこの未定義ルーティングでクラスを使用する
は、トリックを行います
// router.js
const ExampleController = require('./ExampleController');
const instanceOfExampleController = new ExampleController();
// Require express and other dependencies
app.post('/post-to-login', instanceOfExampleController.login) // An error appears inside the method
とコントローラ
// My Controller
class ExampleController {
// Private method
myPrivateMethod(info) {
return info.toUpperCase();
}
login(req, res, next) {
console.log('----------------------');
console.log(this); // Here "this" equal of undefined!
console.log('----------------------');
const someValue = this.myPrivateMethod(req.body.info); // Not work!
res.send(someValue);
};
}
驚いたことに、あなたは提案方法のどれもが私を助けていません。 "this"の呼び出しコンテキストは未定義です – AZawalar