Javascript NodeJS 导出变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27614020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:23:54  来源:igfitidea点击:

NodeJS export variable

javascriptnode.js

提问by mdv

I am writing a nodejs moduleand I need to pass variable data from the main file to the functions, I am doing this:

我正在编写一个nodejs 模块,我需要将变量数据从主文件传递给函数,我这样做:

var region;
var api_key;

exports.region = region;
exports.api_key = api_key;


module.exports = {

 getSummonerId:  function(sum, callback) {

   var summoners = {};
   var summoner = sum.replace(/\s+/g, '');

   request("https://na.api.pvp.net/api/lol/"+region+"/v1.4/summoner/by-name/"+summoner+"?api_key="+api_key, function(error, response, body) {
         summoners[summoner] = JSON.parse(body);
         callback(summoners[summoner][summoner].id);
   });
 }
}

And in the main file:

主文件中

var lol = require('./apiwrapper.js');

lol.api_key = "example";
lol.region  = "las";


lol.getChampions(function(data) {
   console.log(data);
})

But from apiwrapper.jsthose two variables value is always "undefined".

但是从apiwrapper.js这两个变量的值总是“ undefined”。

Can some point me in the right direction?

有人能指出我正确的方向吗?

Thanks in advance.

提前致谢。

回答by Felix Kling

The value that is imported to the other module is module.exports. So, what you assign to module.exportsis exported. Whatever was assigned earlier to it is lost.

导入到另一个模块的值为module.exports。因此,您分配给的内容module.exports已导出。之前分配给它的任何东西都丢失了。

The relation between module.exportsand exportsis that they refer to the same object initially:

module.exports和之间的关系exports是它们最初指的是同一个对象:

var exports = module.exports = {};

So, assigning a property to either of them mutates the same object. However, you are assigning a new object to module.exports, so now both of them reference different objects.

因此,将属性分配给它们中的任何一个都会改变同一个对象。但是,您正在为 分配一个新对象module.exports,因此现在它们都引用了不同的对象。

A simple solution is to assign the new object to exportsas well and then assign the other properties:

一个简单的解决方案是将新对象exports也分配给,然后分配其他属性:

exports = module.exports = {...};
exports.region = region;

If you want to keep the order of the statements, then you have to extend the default exports object, instead of creating a new one:

如果你想保持语句的顺序,那么你必须扩展默认的导出对象,而不是创建一个新的对象:

Object.assign(exports, { ... });

回答by Rana

Why not using:

为什么不使用:

 module.exports = {
 region: my_region,
 api_key: my_api_key,
 getSummonerId:  function(sum, callback) {

   var summoners = {};
   var summoner = sum.replace(/\s+/g, '');

   request("https://na.api.pvp.net/api/lol/"+region+"/v1.4/summoner/by-name/"+summoner+"?api_key="+api_key, function(error, response, body) {
         summoners[summoner] = JSON.parse(body);
         callback(summoners[summoner][summoner].id);
   });
 }
}

in your case, "module.exports" is overwriting the previously exported variables. Which is the reason you are getting undefined for those.

在您的情况下,“module.exports”正在覆盖之前导出的变量。这就是您对这些未定义的原因。