最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js export variable - Stack Overflow

programmeradmin2浏览0评论

I am writing a Node.js module and I need to pass variable data from the main file to the functions. I am doing this:

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("/" + 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.js file, those two variables' value are always "undefined".

How can I fix this?

I am writing a Node.js module and I need to pass variable data from the main file to the functions. I am doing this:

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.js file, those two variables' value are always "undefined".

How can I fix this?

Share Improve this question edited Oct 3, 2020 at 23:44 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 23, 2014 at 4:18 mdvmdv 9454 gold badges14 silver badges28 bronze badges 4
  • Please refer this: stackoverflow.com/questions/7137397/… – BlackMamba Commented Dec 23, 2014 at 4:21
  • You cann't use exports.region and module.exports at the same time. – BlackMamba Commented Dec 23, 2014 at 4:21
  • What is getChampions? – thefourtheye Commented Dec 23, 2014 at 4:22
  • and if I do module.exports.region? – mdv Commented Dec 23, 2014 at 4:35
Add a comment  | 

2 Answers 2

Reset to default 13

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

The relation between module.exports and exports is that they refer to the same object initially:

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.

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

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, { ... });

Use:

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.

发布评论

评论列表(0)

  1. 暂无评论