0

I have following code:-

class FeedbackController {

  public homePage(req, res){
    this.test();
    res.send('Welcome to feedback service');
  }
 
  private test(){
    console.log('test called');
  }
}

export default new FeedbackController();

This is how it is called:-

import FeedbackController from '../controller/feedbackController';

    const routes = (app) => {
        app.route('/')
        .get(FeedbackController.homePage);

    };

    export default routes;

I get error :-

TypeError: Cannot read property 'test' of undefined

What is wrong

Masi Boo
  • 635
  • 1
  • 10
  • 24

1 Answers1

0

This is not a TS problem, check how this works in JS here.

As a solution replace routes with

.get((req, res) => FeedbackController.homePage(req, res))

or

.get(FeedbackController.homePage.bind(FeedbackController))
Voskanyan David
  • 987
  • 6
  • 6