0

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
}
NicklasN
  • 484
  • 1
  • 7
  • 20

3 Answers3

5

Working as expected.

Take a look here. You just need to rectify some syntactic errors.

Test.js

class Test{

  test(){
    console.log('hello');
  }

  dependentMethod(){
    this.test();
  }
}

module.exports = Test;

Test1.js

const fileR = require('./Test.js');

const validator = async function(){

 const fr = new fileR();
 const serverTest = await fr.dependentMethod();

};

validator();

Output:

> hello
Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
  • Remove parenthesis while declaring a class. eg. `class Test`. – Hardik Shah Aug 13 '18 at 08:31
  • It does not work here, I use it as middleman for my get. Could there be something there? – NicklasN Aug 13 '18 at 09:13
  • Your `Test` class file contains `module.export` instead of `module.exports`, maybe that's the culprit? – Sander Aug 13 '18 at 09:17
  • @NicklasN, as described `sander` mentioned, maybe that is the issue. – Hardik Shah Aug 13 '18 at 09:19
  • This is a mistake, the original code has exports. And it works without the local call – NicklasN Aug 13 '18 at 09:21
  • Just set it up exactly like this, but it just throws the same error. – NicklasN Aug 13 '18 at 09:40
  • still get `TypeError: Cannot read property 'test' of undefined`? – Hardik Shah Aug 13 '18 at 10:16
  • `TypeError: Cannot read property 'test' of undefined` error should not come now? You might have some different issue, which may be not in this question. – Hardik Shah Aug 13 '18 at 10:34
  • Same error IF the function is INSIDE the class. If I put the function OUTSIDE of the class scope but in the same file it works fine – NicklasN Aug 13 '18 at 10:49
  • aah, maybe because you are not creating object correct. check this. `const test = require(__dirname + '/server/Test');` and you are initializing wrong `const test = new Test();` .... What you should do is, `const Test = require(__dirname + '/server/Test');` and then `const test = new Test();` will work. Take care about the capitalization of `Test` and `test` both are different. – Hardik Shah Aug 13 '18 at 11:20
  • This is also done correct in my version, sorry. Lots of mistakes cause I typed it manually on here. The object gets invoked correctly – NicklasN Aug 13 '18 at 11:34
  • As the answer above suggests its something about the middleware usage – NicklasN Aug 13 '18 at 11:34
  • Can't help until your actual code. Seems mysterious to me now :( – Hardik Shah Aug 13 '18 at 11:54
2
 class Test{

   test(){
    console.log('hello');
   }

   let dependentMethod = ()=>{
     this.test();
   }
  }

 module.exports = Test;

either manually bind this or use arrow functions which will bind this to the class object

Rahul Somaraj
  • 251
  • 2
  • 9
1

Man, you're not showing you real code to us.

I feel that in real life you are using test.dependentMethod as middleware. And it aclually is using standalone function loosing the context. Thats's why you have an error Cannot read property 'test' of undefined.

The solution is either using test.dependentMethod.bind(test) or the code from your Edit section, where you created separate validator function and using class instances correctly.

  • It is indeed the middleware part doing something weird. I cannot call it like the way you prescribe. It says test is undefined still – NicklasN Aug 13 '18 at 11:25