0

This is my file loginHandler.js

class LoginHandler {
 merchantId = '';
    returnURLForIframe(req, res) {
      merchantId = req.params.merchantId;
    }  
}

module.exports = new LoginHandler();

I want to access the variable merchantId on another file

const loginHandler  = require('./loginHandler')
class ResponseHandler {
    
    getResponseFromCOMM(options,token, res){
        console.log(loginHandler.merchantId)
    }
}

But merchantId is undefined. Can you please tell me what I am doing wrong?

Here you can see the code on Glitch = https://glitch.com/edit/#!/turquoise-spiky-chrysanthemum

4 Answers4

2

I solved it by adding it to an environment variable on loginHandler.js

process.env.MERCHANT_ID = req.params.merchantId

and then on responseHandler.js, I accessed that variable

merchantId : process.env.MERCHANT_ID

1

My loginhanderler.js

class LoginHandler {
  merchantId = '';
  returnURLForIframe(req, res) {
    this.merchantId = req.params.merchantId;
  }
}

module.exports = new LoginHandler();

My index.js

let loginHandler = require('./loginhandler');

let req = {
  params: {
    merchantId: 'a test',
  },
};

loginHandler.returnURLForIframe(req);

console.log(loginHandler.merchantId);
Karlan
  • 353
  • 1
  • 2
  • 10
0

You can define it as an object key

class LoginHandler {
  constructor() {
    this.merchantId = '';     
  }

    returnURLForIframe(req, res) {
      this.merchantId = req.params.merchantId;
    }  
}
Sarath P S
  • 131
  • 5
0

new LoginHandler

class LoginHandler {
    merchantId = "";
  returnURLForIframe(req, res) {
    this.merchantId = req.params.merchantId;
  }
}

module.exports = new LoginHandler();

FOR FUTURE REFERENCE (also for myself)

It was confusing to detect what the error was, so for me it was helpful to change the name of the variable:

class LoginHandler {
    other= "";
  returnURLForIframe(req, res) {
    other = req.params.merchantId;
  }
}

module.exports = new LoginHandler();

Then I saw that the error was ReferenceError: other is not defined and could solve it.

Also, besides logging, it was needed to call returnURLForIframe to see the error

const loginHandler = require("./loginHandler");
class ResponseHandler {
  getResponseFromCOMM(options, token, res) {
    loginHandler.returnURLForIframe({ params: { merchantId: "lalala" } });
    console.log(loginHandler);
  }
}
let rh = new ResponseHandler();
rh.getResponseFromCOMM("foo", "bar", "baz");
malarres
  • 2,941
  • 1
  • 21
  • 35
  • I updated it with `this.merchantId = req.params.merchantId;` But I'm getting `Error is TypeError: Cannot set property 'merchantId' of undefined at returnURLForIframe (C:\Users\Acer\angularProjects\pamSetup\comm-channel-node-gateway\handler\loginHandler.js:10:23)` – Nehal Jaisalmeria Aug 24 '20 at 11:37
  • @NehalJaisalmeria this can be related to how you call the function. `loginHandler.returnURLForIframe({ params: { merchantId: "lalala" } });` will work – malarres Aug 24 '20 at 11:38
  • https://glitch.com/edit/#!/turquoise-spiky-chrysanthemum check this on Glitch. – Nehal Jaisalmeria Aug 24 '20 at 12:05
  • Maybe that's another question to ask ;-) Also because you'll have the opportunity to tag `expressJS` and get experts on the matter – malarres Aug 24 '20 at 12:16
  • I got this error .. `Error is TypeError: loginHandler.returnURLForIframe is not a function at ResponseHandler.getResponseFromCOMM ` Please help me out. Not able to figure the issue. I just want to export the `merchantId` from `loginHandler.js` to `responseHandler.js` – Nehal Jaisalmeria Aug 24 '20 at 16:05