You need to use the binding function to save the scope thiswhen calling the method:
let user = new User();
router.get("/", user.test.bind(user));
Or you can do it in the constructor User:
export class User {
constructor() {
this.test = this.test.bind(this);
}
test(req, res, next) {
...
}
}
Another option is to use the function:
let user = new User();
router.get("/", (req, res, next) => user.test(req, res, next));
source
share