6

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?

4
  • Please refer this: stackoverflow.com/questions/7137397/… Commented Dec 23, 2014 at 4:21
  • You cann't use exports.region and module.exports at the same time. Commented Dec 23, 2014 at 4:21
  • What is getChampions? Commented Dec 23, 2014 at 4:22
  • and if I do module.exports.region? Commented Dec 23, 2014 at 4:35

2 Answers 2

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, { ... });
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

yup, exactly. Just try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.