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?