如何导出Node.js中的类中的变量 [英] How to export a variable which is in a class in Nodejs
本文介绍了如何导出Node.js中的类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的文件loginHandler.js
This is my file loginHandler.js
class LoginHandler {
merchantId = '';
returnURLForIframe(req, res) {
merchantId = req.params.merchantId;
}
}
module.exports = new LoginHandler();
我想访问另一个文件上的变量 merchantId
I want to access the variable merchantId
on another file
const loginHandler = require('./loginHandler')
class ResponseHandler {
getResponseFromCOMM(options,token, res){
console.log(loginHandler.merchantId)
}
}
但是商人ID未定义.你能告诉我我在做什么错吗?
But merchantId is undefined. Can you please tell me what I am doing wrong?
在这里您可以在Glitch = https://glitch.com上看到代码/edit/#!/turquoise-spiky-菊花
Here you can see the code on Glitch = https://glitch.com/edit/#!/turquoise-spiky-chrysanthemum
推荐答案
我通过将其添加到loginHandler.js的环境变量中来解决了该问题
I solved it by adding it to an environment variable on loginHandler.js
process.env.MERCHANT_ID = req.params.merchantId
然后在responseHandler.js上,我访问了该变量
and then on responseHandler.js, I accessed that variable
merchantId:process.env.MERCHANT_ID
这篇关于如何导出Node.js中的类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文