So I have a class with functions where one is dependent on another. This class is exported with module. According to anything I can find I should be able to use "this" but that throws an error.
Example:
class Test{
test(){
console.log('hello');
}
dependentMethod(){
this.test();
}
}
module.exports = Test;
This however throws these errors in node:
(node:69278) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'test' of undefined
(node:69278) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:69278) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'test' of undefined
It will work fine if I put the function outside the class though. Can anyone explain why this fails? :)
EDIT:
This is the code in server.js (simplified for the example) that uses the class:
const test = require(__dirname + '/server/Test');
const validator = async function(req, res, next){
const test = new test();
const serverTest = await test.dependentMethod();
next();
};
app.get('/Response/:id/:is/:userId/:hash', validator, async function (req, res, next) {
//does smth
}
Used seperately does not work either
const test = new Test();
app.get('/Response/:id/:is/:userId/:hash', Test.dependentMethod, async function (req, res, next) {
//Same error
}