DEV Community


Discussion on: 💪 Express.js on steroids: an OOP way for organizing Node.js project [starring TypeScript]

ilhamtubagus profile image
Ilham Tubagus Arfian • Edited

Good article, very helpful. I wonder why i got error when i'm trying to access another function within my class controller. For example i have async handleLogin function that handle login request, inside this function i'm calling another function within the same class controller.

mbisurgisoshace profile image
mbisurgisoshace • Edited

That is because the functions are not being called like a regular class method, so for intance AuthController.method(). They are being called as callback functions by express, so the 'this' keyword reference is lost. The way to solve it is to bind the 'this' context. So for example in this case, where you have handler: this.handleLogin, you just need to do this.handleLogin.bind(this). Hope this was helpful!