Search code examples
javascriptnode.jsexpressnodejs-server

How to export a variable which is in a class in Nodejs


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


Solution

  • 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