Having an issue with regard to calling the function in typescript
I have a class
export class Test{
constructor(){}
public func1(req:request,res:Response) {
// call method
let func2 = this.Func2();
}
private Func2():string{
return "Hello";
}
}
export default new Test();
I have an express Router class
import {Router} from "express";
import Test from '../Handlers/Test';
export class UserRouter {
router: Router;
constructor() {
this.router = Router();
}
Routes() {
this.router.route('/user').post(Test.Func1);
return this.router;
}
}
export default new UserRouter().Routes();
The issue is when I try to call the method Func2 using this.Func2() in Func1() method i get an error stating cannot call Func2 of undefined. I either have to call it in 2 ways
1) `new Test().Func2() inside Func1() method as
public Func1(req:request,res:Response){
let func2 = new Test().Func2();
}
2) make Func2() as static and then call it using Test.Func2()
public Func1(req:request,res:Response){
let func2 = Test.Func2();
}
private static Func2():string{
return "Hello";
}
why can't I just call it using this.Func2() ?
Is it because I am exporting new of Test() to the router method and that object is no longer in memory?
Func2is static then it doesn't exist onthis, only onTest.