I have a typescript class containing all my express page functions, but get an 'undefined' error, when I try to access its class' member variable using this
:
class CPages {
private Version: string;
constructor(version: string) {
this.Version = version;
console.log(this.Version); // <- now 'this' refers to the class CPages. Output: 1.0
}
public async sendPage(req: express.Request, res: express.Response) {
console.log(this.Version); // <- now 'this' is undefined and so is this.Version. Output: Cannot read property 'Version' of undefined
res.render('page', {
version: this.Version
});
}
}
/* ... */
const Pages = new CPages("1.0");
App.get('/', Pages.sendPage);
I had a look at the behaviour of arrow functions (here), seem to be similar. But unlike the examples there I can neither access the main programm nor the class by using this
. So, how can I access the version variable without sending it as a parameter?
ps: The code is a bit simplified and the App.get(...);
is in a class itself.