Node.js export variable - javascript

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?

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.

Related

AWS Lambda function global variables

I am writing an AWS Lambda function in JavaScript (Node.js) that interacts with CodeCommit through the AWS SDK.
The communication between the services works as expected, I am getting data within the CodeCommit function, but the issue I am facing appears when I want to use this data outside of the function.
I have tried two approaches:
1. Global Variable
Code:
var aws = require('aws-sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015-04-13' });
var repoName = ''; // Declared my global variable here
exports.handler = function(event, context) {
var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
var repository = "my-repository"
var params = {
repositoryName: repository
};
codecommit.getRepository(params, function(err, data) {
if (err) {
console.log(err);
var message = "Error getting repository metadata for repository " + repository;
console.log(message);
context.fail(message);
} else {
console.log('Repository Name:', data.repositoryMetadata.repositoryName); // Shown with data
repoName = data.repositoryMetadata.repositoryName; // Setting the variable
console.log('Account Id:', data.repositoryMetadata.accountId); // Shown with data
}
});
console.log(repoName); // Shown as blank in the output
};
Output:
The last written "console.log" is the first to print in the execution results, but shows blank. The two other console.log (within the functions) are then printed, and they show the data.
2. Function
Code:
var aws = require('aws-sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015-04-13' });
exports.handler = function(event, context) {
var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
var repository = "my-repository"
var repoData = getRepository(repository)
console.log('Repository Name:', repoData.repositoryName);
console.log('Account Id:', repoData.accountId);
};
function getRepository(repository) {
var params = {
repositoryName: repository
};
codecommit.getRepository(params, function(err, data) {
if (err) {
console.log(err);
var message = "Error getting repository metadata for repository " + repository;
console.log(message);
context.fail(message);
} else {
var repoData = {};
repoData.repositoryName = data.repositoryMetadata.repositoryName;
repoData.accountId = data.repositoryMetadata.accountId;
console.log(repoData); // Shows output in execution results when lines 11 & 12 are commented
return repoData;
}
});
}
Output:
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'repositoryName' of undefined",
"trace": [
"TypeError: Cannot read property 'repositoryName' of undefined",
" at Runtime.exports.handler (/var/task/index.js:57:46)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
Conclusion
None of those approaches worked. The data is always visible within the function but never outside of it. I suspect that the code outside of the function executes before the function itself, and I wonder if I could make the code to wait that the function has been executed before doing the console.log (and other actions after it). Or maybe I am wrong on another level?
You are using a callback model, in which case the console.log in the first example is being hit before the code in the callback. A better option would be to use async/await.
var aws = require('aws-sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015-04-13' });
var repoName = ''; // Declared my global variable here
exports.handler = async function(event, context) {
var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
var repository = "my-repository"
var params = {
repositoryName: repository
};
var data = await codecommit.getRepository(params).promise();
console.log('Repository Name:', data.repositoryMetadata.repositoryName); // Shown with data
repoName = data.repositoryMetadata.repositoryName; // Setting the variable
console.log('Account Id:', data.repositoryMetadata.accountId); // Shown with data
console.log(repoName);
};
Notice that I'm not catching the error here, but if you wanted to you can use a try/catch block. Just be sure you throw a new error in that case if you want the function to fail.

NodeJS/ES6: Cannot set property of undefined

Using NodeJS/ES6 I have created a MongoDB connector class.
class DBClient {
constructor(host, port) {
this.host = host;
this.port = port
this.dbConnection = null;
}
buildConnectionString() {
return 'mongodb://' + this.host + ':' + this.port;
}
connect() {
var connectionString = this.buildConnectionString();
console.log('[MongoDB] - Connecting to instance # ' + connectionString);
var DBConnection = MongoClient.connect(connectionString, function(error, db) {
if (error) {
console.log('[MongoDB] - Error connecting to instance');
console.log(error);
}
else {
console.log('[MongoDB] - Connection Successful');
this.dbConnection = db;
}
});
}
}
Which is then being created in a different file like so
var client = new DBClient('127.0.0.1', '1337');
client.connect();
When the database is connected to, NodeJS crashes when it reaches this.dbConnection = db;, stating TypeError: Cannot set property 'dbConnection' of undefined.
I'm pretty sure it has something to do with being used in a callback, which is screwing up the scope. How can I get around this though? Wouldn't any operation from the callback scope be isolated and unable to reference this?
Also, as a side question, is this a bad code practice to initialize a null property like I'm doing in the constructor? If so, what would be a more proper way of doing it?
Indeed if you want to keep your scope use lambda instead like :
var DBConnection = MongoClient.connect(connectionString, (error, db) =>
{
...
});
if you have to keep your function because of your transpilation settings or the lib does not support lambda, save your scope in a variable like :
var self = this;
var DBConnection = MongoClient.connect(connectionString, function(error, db)
{
... self.dbConnection = db;
});

Javascript inherited object overwrites other inherited object

Can someone please explain what the correct way is to have multiple objects inherit from a parent and have their own prototype functions? I'm trying to do this in nodeJS.
I have these files.
ParserA_file
var ParentParser = require('ParentParser_file');
module.exports = ParserA;
ParserA.prototype = Object.create(ParentParser.prototype);
ParserA.prototype.constructor = ParserA;
ParserA.prototype = ParentParser.prototype;
function ParserA(controller, file) {
ParentParser.call(this, controller, file);
this.controller.log('init --- INIT \'parser_A\' parser');
this.date_regex = /([0-9]{1,2})?([A-Z]{3})?([0-9]{2})? ?([0-9]{2}:[0-9]{2})/;
this.date_regex_numeric = /(([0-9]{1,2})([0-9]{2})([0-9]{2}))? ?([0-9]{2}:[0-9]{2})?/;
this.date_format = 'DDMMMYY HH:mm';
}
ParserA.prototype.startParse = function() {
console.log('Starting parse for A');
}
ParserB_file
var ParentParser = require('ParentParser_file');
module.exports = ParserB;
ParserB.prototype = Object.create(ParentParser.prototype);
ParserB.prototype.constructor = ParserB;
ParserB.prototype = ParentParser.prototype;
function ParserB(controller, file) {
ParentParser.call(this, controller, file);
this.controller.log('init --- INIT \'parser_B\' parser');
this.date_regex = /([0-9]{1,2})?([A-Z]{3})?([0-9]{2})? ?([0-9]{2}:[0-9]{2})/;
this.date_regex_numeric = /(([0-9]{1,2})([0-9]{2})([0-9]{2}))? ?([0-9]{2}:[0-9]{2})?/;
this.date_format = 'DDMMMYY HH:mm';
}
ParserB.prototype.startParse = function() {
console.log('Starting parse for B');
}
ParentParser_file
ParentParser = function(controller, file) {
if (!controller) {
throw (new Error('Tried to create a Parser without a controller. Failing now'));
return;
}
if (!file ) {
throw (new Error('Tried to create a Parser without a file. Failing now'));
return;
}
this.controller = null;
this.file = null;
}
module.exports = ParentParser;
Now I require them both in my node app
var ParserA = require('ParserA_file');
var ParserB = require('ParserB_file');
Now, when only one parser is loaded the there is no problem, however, loading them both into my node app and starting parser A
var parser = new ParserA(this, file);
parser.startParse()
returns
init --- INIT 'parser_B' parser'
Now for the question, how come ParserB's function startParse overwrites the startParse from ParserA?
That's because they refer to the same prototype object.
ParserA.prototype = ParentParser.prototype;
...
ParserB.prototype = ParentParser.prototype;
ParserA.prototype === ParserB.prototype; // true
Remove those two lines (which are overwriting the two lines above them anyway) and you'll be good to go.

Node.js require caching

I am really stuck by nodejs cache system. I have this structure for my project :
Project/
apps/
jobs_processor/
app.js
processors.js
processors/
libs/
queue_manager.js
queue_manager.js require processors.js
var processors = require("../apps/jobs_processor/processors.js");
app.js require also processor.js
var processors = require("./processors.js");
If I take into account the documentation, I must have the same path may be to obtain the same object, is that right ? If so, how can I achieve that (have the same path) ?
Thanks.
EDIT:
If found a solution to my problem.
Here is the first version of queue_manager.js file
var _ = require("lodash");
var Utils = require("./utilities");
var Processors = require("../apps/jobs_processor/processors");
var Logger = require("./logger");
var QUEUES_CACHE = {};
exports.createJob = createJob;
exports.getCacheObject = getCacheObject;
function createJob(name, data) {
var cacheId = name.replace(/ /g, "_");
Logger.info("Cache ID: " + cacheId);
if (!QUEUES_CACHE[ cacheId ]) {
_.each(Processors, function (processor) {
Logger.debug("PROCESSOR NAME: " + processor.name);
Logger.debug("JOB NAME: " + name);
if (processor.name === name)
QUEUES_CACHE[ cacheId ] = processor;
});
if (!QUEUES_CACHE[ cacheId ])
throw new Error("Processor for job \"" + name + "\" not found.");
}
Logger.debug(Object.keys(QUEUES_CACHE));
return QUEUES_CACHE[ cacheId ].queue.add(data);
}
function getCacheObject() {
return QUEUES_CACHE;
}
And now the last version of the same file
var _ = require("lodash");
var Utils = require("./utilities");
var Logger = require("./logger");
exports.createJob = createJob;
function createJob(name, data) {
var Processors = require("../apps/jobs_processor/processors");
var processor;
_.each(Processors, function (element) {
Logger.debug("Processor name: " + element.name);
if (element.name === name)
processor = element;
});
return processor.queue.add(data);
}
Each time that i called createJob method, I require the processors module which is an array of each job processor that I have created.
Node.js will resolve the path before caching the module.
As long as your relative paths resolve to the same absolute path on disk, you're fine.

Node.js Combine module/exports from multiple files

I wish to split a large configuration .js file into multiple smaller files yet still combine them into the same module. Is this common practice and what is the best approach so that the module would not need extending when new files are added.
An example such as but not needing to update math.js when a new file is added.
math
- add.js
- subtract.js
- math.js
// add.js
module.exports = function(v1, v2) {
return v1 + v2;
}
// subtract.js
module.exports = function(v1, v2) {
return v1 - v2;
}
// math.js
var add = require('./add');
exports.add = add;
var subtract = require('./subtract');
exports.subtract = subtract;
// app.js
var math = require('./math');
console.log('add = ' + math.add(5,5));
console.log('subtract =' + math.subtract(5,5));
You can use the spread operator ... or if that doesnt work Object.assign.
module.exports = {
...require('./some-library'),
};
Or:
Object.assign(module.exports, require('./some-library'));
If your NodeJs allows the spread (...) operator (check it here), you can do:
module.exports = {
...require('./src/add'),
...require('./src/subtract')
}
You can do this
// math.js
module.exports = function (func){
return require('./'+func);
}
//use it like this
// app.js
var math = require('./math');
console.log('add = ' + math('add')(5,5));
console.log('subtract =' + math('subtract')(5,5));
You could create a subfolder to assign functions and other objects automaticly.
mymodule.js
const pluginDir = __dirname + '/plugins/';
const fs = require('fs');
module.exports = {};
fs.readdirSync(pluginDir).forEach(file => {
Object.assign(module.exports,require(pluginDir + file));
});
plugins/mymodule.myfunction.js
module.exports = {
myfunction: function() {
console.log("It works!");
}
};
index.js
const mymodule = require('mymodule.js');
mymodule.myfunction();
// It works!

Categories