Calling a local method inside a class in Node.js and module.export

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Calling a local method inside a class in Node.js and module.export



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





How do you import and use the module?
– Patrick Hund
Aug 13 at 8:13





Please show the promise code that causes the rejection. The class itself is fine.
– Bergi
Aug 13 at 8:24





remove () next to class Test
– Prodigle
Aug 13 at 8:41





Removed it. The original doesnt have it either
– NicklasN
Aug 13 at 9:12





const test = new test(); should throw a TDZ reference error - you need to use different names
– Bergi
Aug 13 at 14:02


const test = new test();




2 Answers
2



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





Thanks, please see my edit :)
– NicklasN
Aug 13 at 8:30





Remove parenthesis while declaring a class. eg. class Test.
– Hardik Shah
Aug 13 at 8:31


class Test





updated answer as per your update.
– Hardik Shah
Aug 13 at 8:34





It does not work here, I use it as middleman for my get. Could there be something there?
– NicklasN
Aug 13 at 9:13





Your Test class file contains module.export instead of module.exports, maybe that's the culprit?
– Sander
Aug 13 at 9:17


Test


module.export


module.exports



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.


test.dependentMethod


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.


test.dependentMethod.bind(test)





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 at 11:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS