This is probably a noob JavaScript question, but I'm looking to know if my solution to a problem I am having is 'correct'
I have created the following sample application that recreates my error:
Firstly in index.js
var processor = require('./fileProcessor/processor.js');
var container = {
source: "source.txt",
destination: "destination.txt"
};
new processor().process(container);
I create my container object which has the name of the source file and the name of the destination file. This is passed into the process function of the processor:
var fileProcessor = require('./fileProcessor.js');
module.exports = function Processor() {
this.process = function(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, this.process);
} else {
file.write(container, this.process);
}
}
};
};
As you can see this calls the read and write functions passing in the container and the process function as the callback, the fileProcessor looks like this:
var fs = require('fs');
module.exports = function() {
this.read = function(container, callback) {
fs.readFile(container.source, function (err, data) {
if(err) throw err;
container.body = data;
callback(container);
});
};
this.write = function(container, callback) {
fs.writeFile(container.destination, container.body, function(err) {
if(err) {
return console.log(err);
}
container.finished = true;
callback(container);
});
};
};
In simple terms the processor calls file.read, which reads the file and calls back into the process function, which then calls the write function. However at the end of the write function an error is thrown:
callback(container);
^
TypeError: object is not a function
Obviously when passing in this.process to file.write(container, this.process); the this isn't the this I intend it to be!
If I update my processor by adding a processFunction variable:
var fileProcessor = require('./fileProcessor.js');
module.exports = function Processor() {
var processFunction = function(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, processFunction);
} else {
file.write(container, processFunction);
}
}
};
this.process = function(container) {
processFunction(container);
};
};
Everything works fine. Is this a good way to do this or is there a better solution?
I think this is a fine way to do it. There is one possible modification that you might make. Since you are creating a new name in your scope just for the purpose of recursing, you could just name your function and refer to it by its name inside of the function.
module.exports = function Processor() {
this.process = function processFunction(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, processFunction);
} else {
file.write(container, processFunction);
}
}
};
};
Then you can avoid creating a name (processFunction) that will be visible outside the function.
Take a look here for reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#Named_function_expression
Related
I would like to pass the current context or an attribute to functions in async.waterfall. How to:
pass this
an attribute (here options object)
This is what I already have:
var _authenticate = function (cbAsync) {
var licenseId = options.licenseId; //options is undefined
};
module.exports = new Command('createOrga')
.description('creates an organization')
.option('-f, --file <file>', 'the structure file including full path')
.action(function (options) {
options.confirm = options.confirm || true; // No confirmation needed!
options.organizationUUID = (uuid.v4()).toUpperCase();
options.licenseId = (uuid.v4()).toUpperCase();
//How to pass options object to _authenticate function????
async.waterfall([ _authenticate ], function(err) {
if ( err ) {
console.warn('Error in creating new organization: ',err);
}
else {
console.info('Successfully created new organization: ' + organizationUUID);
}
});
}
}
You could use Function.prototype.bind() to pass variables.
async.waterfall([ _authenticate.bind(undefined, options)], function(err) {
//your code
});
Because the bound variables are pass first, your callback then has to look like this:
var _authenticate = function (options, cbAsync) {
var licenseId = options.licenseId; //options is undefined
};
I just started using Mocha. I'm writing a test that requires file cleanup, whether the test succeeds or not. Here's what I'm kind of looking for:
describe('example',function(){
fs.writeFile(file,'foo', function(){
it('should succeed', function(done){
done();
describe('example2', function(){
it('should do something awesome', function(done){
done();
});
})
}
}
});
});
});
afterAllTests(function(){
fs.unlink(file);
})
What I'm looking for is to delete all my created files during the test. Right now, I delete them after my last test completes. But if any test fails, the files don't get deleted. I did look into after(), but it only runs after my first test. The only other option that I can see is doing afterEvery(), and setting a variable like var testFinished = false; at the beginning and setting the value to true once completed. Is this the best way?
Thanks!
I wrote a file system wrapper that tracks the files written and can then delete all of them. Note: I only wrapped the functions I was using. You can easily add more.
// trackedfs.js
var fs = require('fs');
function deleteNoFail(filePath) {
if (filePath && fs.existsSync(filePath)) {
if (fs.lstatSync(filePath).isDirectory()) {
fs.rmdirSync(filePath);
} else {
fs.unlinkSync(filePath);
}
}
};
function makeTrackedFS(options) {
options = options || {};
var f = options.fs || fs;
var files = [];
var folders = [];
var links = [];
function addFile(file) {
files.push(file);
}
function addFolder(folder) {
folders.push(folder);
}
function addLink(link) {
links.push(link);
};
// Expose these in case we want to manually add stuff
// like if we shell out to another program we can add
// its output
f.addFile = addFile;
f.addFolder = addFolder;
f.addLink = addLink;
f.mkdirSync = function(origFn) {
return function() {
addFolder(arguments[0]);
return origFn.apply(f, arguments);
};
}(f.mkdirSync);
f.symlinkSync = function(origFn) {
return function() {
addLink(arguments[1]);
return origFn.apply(f, arguments);
};
}(f.symlinkSync);
f.writeFileSync = function(origFn) {
return function() {
addFile(arguments[0]);
return origFn.apply(f, arguments);
};
}(f.writeFileSync);
function deleteList(list) {
list.reverse(); // because we want files before dirs
list.forEach(function(entry) {
deleteNoFail(entry);
});
};
// call to delete everything
f.cleanup = function(options) {
deleteList(links);
deleteList(files);
deleteList(folders);
links = [];
files = [];
folders = [];
};
};
exports.makeTrackedFS = makeTrackedFS;
I can now wrap fs with
var trackedfs = require('trackedfs');
trackedfs.makeTrackedFS();
and set for cleanup with this
function cleanUpOnExit() {
fs.cleanup();
};
function cleanUpOnExitAndExit() {
cleanUpOnExit();
process.exit();
}
process.on('exit', cleanUpEncodersOnExit);
process.on('SIGINT', cleanUpEncodersOnExitAndExit);
process.on('uncaughtException', cleanUpEncodersOnExitAndExit);
Here's a test
var trackedfs = require('../lib/trackedfs');
var fs = require('fs');
trackedfs.makeTrackedFS();
function cleanUpOnExit() {
fs.cleanup();
};
function cleanUpOnExitAndExit() {
cleanUpOnExit();
process.exit();
}
process.on('exit', cleanUpOnExit);
process.on('SIGINT', cleanUpOnExitAndExit);
process.on('uncaughtException', cleanUpOnExitAndExit);
describe("trackedfs", function() {
it('makes a directory', function() {
fs.mkdirSync('foo');
});
it('writes a file', function() {
fs.writeFileSync("foo/bar", "hello");
});
});
Run it and there will be no foo folder or foo/bar file when done.
The one issue is that it probably can't delete files that are still opened so if you open a file and then crash your test it might not be able to delete it.
I'm trying to understand why I get "variable may not have been initialized" when I test (variable === "some text") but I dont get when I used (typeof passwordHashOrg !== 'undefined')
The working code:
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (typeof passwordHashOrg !== 'undefined') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
and the code that I get "variable may not have been initialized" :
checkPass('share', function (error, response) {
"use strict";
if (error) {
console.log(error);
} else {
console.log(response);
}
});
function checkPass(username, callback) {
"use strict";
// Require
var fs = require('fs');
// Static Parameters
var usernameInput = username;
// Open shadow file
fs.readFile('/etc/shadow', function (error, file) {
if (error) {
return callback(error); // file does not exit
}
// file is a buffer, convert to string and then to array
var shadowArray = file.toString().split('\n');
var passwordHashOrg;
shadowArray.forEach(function (line) {
var shadowLineArray = line.split(":");
var usernameOrg = shadowLineArray[0];
if (usernameOrg === usernameInput) {
passwordHashOrg = shadowLineArray[1];
}
});
if (passwordHashOrg === 'some text') {
callback(null, 'userExist');
} else {
callback(null, 'unknownUser');
}
});
}
The only diferent from the two code is:
if (typeof passwordHashOrg !== 'undefined') {
vs
if (passwordHashOrg === "some text") {
The warning means just what it says - the variable might not have been initialised with a value at the time of the comparison. Since that's the expected behaviour, and basically just what you want to test for, the warning can be safely ignored. Alternatively, use
var passwordHashOrg = null; // or
var passwordHashOrg = undefined; // or whatever default value you want
So why didn't you get the warning when using typeof? Because that's not necessarily evaluating the value of the variable, and might be seen by jshlint as justified on non-initialised variables (it even works on undeclared variables). You'd probably get the same warning if you did compare the value to undefined.
I have a Node.js application that, upon initialisation, reads two tables from an SQL database and reconstructs their relationship in memory. They're used for synchronously looking up data that changes (very) infrequently.
Problem: Sometimes I can't access the data, even though the application reports successfully loading it.
Code:
constants.js
module.exports = {
ready: function () { return false; }
};
var log = sysLog('core', 'constants')
, Geo = require('../models/geo.js');
var _ready = false
, _countries = []
, _carriers = [];
function reload() {
_ready = false;
var index = Object.create(null);
return Geo.Country.find().map(function (country) {
var obj = country.toPlainObject()
, id = obj.id;
delete obj.id;
index[id] = obj;
return Object.freeze(obj);
}).then(function (countries) {
log.debug('Loaded ' + countries.length + ' countries');
_countries = countries;
return Geo.Carrier.Descriptor.find().map(function (carrier) {
var obj = carrier.toPlainObject();
if (obj.country) {
obj.country = index[obj.country];
}
return Object.freeze(obj);
}).then(function (carriers) {
log.debug('Loaded ' + carriers.length + ' carriers');
_carriers = carriers;
});
}).finally(function () {
_ready = true;
});
}
reload().catch(function (err) {
log.crit({ message: 'Could not load constants', reason: err });
process.exit(-42);
}).done();
module.exports = {
reload : reload,
ready : function () { return _ready; },
countries : function () { return _countries; },
carriers : function () { return _carriers; }
};
utils.js
var log = sysLog('core', 'utils')
, constants = require('./constants');
module.exports = {
getCountryByISO: function(iso) {
if (!iso) {
return;
}
if ('string' != typeof iso) {
throw new Error('getCountryByISO requires a string');
}
if (!constants.ready()) {
throw new UnavailableError('Try again in a few seconds');
}
switch (iso.length) {
case 2:
return _.findWhere(constants.countries(), { 'iso2' : iso.toUpperCase() });
case 3:
return _.findWhere(constants.countries(), { 'iso3' : iso.toUpperCase() });
default:
throw new Error('getCountryByISO requires a 2 or 3 letter ISO code');
}
},
getCarrierByCode: function(code) {
if (!code) {
return;
}
if ('string' != typeof code) {
throw new Error('getCarrierByCode requires a string');
}
if (!constants.ready()) {
throw new UnavailableError('Try again in a few seconds');
}
return _.findWhere(constants.carriers(), { 'code' : code });
},
getCarrierByHandle: function(handle) {
if (!handle) {
return;
}
if ('string' != typeof handle) {
throw new Error('getCarrierByHandle requires a string');
}
if (!constants.ready()) {
throw new UnavailableError('Try again in a few seconds');
}
return _.findWhere(constants.carriers(), { 'handle' : handle });
}
};
Use case
if (data.handle) {
carrier = utils.getCarrierByHandle(data.handle);
if (_.isEmpty(carrier)) {
throw new InternalError('Unknown carrier', { handle: data.handle });
}
}
What's going on: All errors are logged; as soon as I see an error (i.e. "Unknown carrier") in the logs, I check the SQL database to see if it should've been recognised. That has always been the case so far, so I check the debug log to see if data was loaded. I always see "Loaded X countries" and "Loaded Y carriers" with correct values and no sign of "Could not load constants" or any other kind of trouble.
This happens around 10% of the time I start the application and the problem persists (i.e. didn't seem to go away after 12+ hours) and seems to occur regardless of input, leading me to think that the data isn't referenced correctly.
Questions:
Is there something wrong in constants.js or am I doing something very obviously wrong? I've tried setting it up for cyclical loading (even though I am not aware of that happening in this case).
Why can't I (sometimes) access my data?
What can I do to figure out what's wrong?
Is there any way I can work around this? Is there anything else I could to achieve the desired behaviour? Hard-coding the data in constants.js is excluded.
Additional information:
constants.reload() is never actually called from outside of constants.js.
constants.js is required only in utils.js.
utils.js is required in app.js (application entry); all files required before it do not require it.
SQL access is done through an in-house library built on top of knex.js and bluebird; so far it's been very stable.
Versions:
Node.js v0.10.33
underscore 1.7.0
bluebird 2.3.11
knex 0.6.22
}).finally(function () {
_ready = true;
});
Code in a finally will always get called, regardless of if an error was thrown up the promise chain. Additionally, your reload().catch(/* ... */) clause will never be reached, because finally swallows the error.
Geo.Country.find() or Geo.Carrier.Descriptor.find() could throw an error, and _ready would still be set to true, and the problem of your countries and carriers not being set would persist.
This problem would not have occurred if you had designed your system without a ready call, as I described in my previous post. Hopefully this informs you that the issue here is really beyond finally swallowing a catch. The real issue is relying on side-effects; the modification of free variables results in brittle systems, especially when asynchrony is involved. I highly recommend against it.
Try this
var log = sysLog('core', 'constants');
var Geo = require('../models/geo.js');
var index;
var _countries;
var _carriers;
function reload() {
index = Object.create(null);
_countries = Geo.Country.find().map(function (country) {
var obj = country.toPlainObject();
var id = obj.id;
delete obj.id;
index[id] = obj;
return Object.freeze(obj);
});
_carriers = _countries.then(function(countries) {
return Geo.Carrier.Descriptor.find().map(function (carrier) {
var obj = carrier.toPlainObject();
if (obj.country) {
obj.country = index[obj.country];
}
return Object.freeze(obj);
});
});
return _carriers;
}
reload().done();
module.exports = {
reload : reload,
countries : function () { return _countries; },
carriers : function () { return _carriers; }
};
constants.reload() is never actually called from outside of
constants.js.
That's your issue. constants.reload() reads from a database, which is an aysnchronous process. Node's require() is a synchronous process. At the time constants.js is required in utils.js and the module.exports value is returned, your database query is still running. And at whatever point in time that app.js reaches the point where it calls a method from the utils module, that query could still be running, resulting in the error.
You could say that requiring utils.js has the side-effect of requiring constants.js, which has the side-effect of executing a database query, which has the side-effect of concurrently modifying the free variables _countries and _carriers.
Initialize _countries and _carriers as unresolved promises. Have reload() resolve them. Make the utils.js api async.
promises.js:
// ...
var Promise = require('bluebird');
var countriesResolve
, carriersResolve;
var _ready = false
, _countries = new Promise(function (resolve) {
countriesResolve = resolve;
})
, _carriers = new Promise(function (resolve) {
carriersResolve = resolve;
});
function reload() {
_ready = false;
var index = Object.create(null);
return Geo.Country.find().map(function (country) {
// ...
}).then(function (countries) {
log.debug('Loaded ' + countries.length + ' countries');
countriesResolve(countries);
return Geo.Carrier.Descriptor.find().map(function (carrier) {
// ...
}).then(function (carriers) {
log.debug('Loaded ' + carriers.length + ' carriers');
carriersResolve(carriers);
});
}).finally(function () {
_ready = true;
});
}
reload().catch(function (err) {
log.crit({ message: 'Could not load constants', reason: err });
process.exit(-42);
}).done();
module.exports = {
reload : reload,
ready : function () { return _ready; },
countries : function () { return _countries; },
carriers : function () { return _carriers; }
};
utils.js
getCarrierByHandle: function(handle) {
// ...
return constants.carriers().then(function (carriers) {
return _.findWhere(carriers, { 'handle' : handle });
});
}
Use case:
utils.getCarrierByHandle(data.handle).then(function (carrier) {
if (_.isEmpty(carrier)) {
throw new InternalError('Unknown carrier', { handle: data.handle });
}
}).then(function () {
// ... next step in application logic
});
This design will also eliminate the need for a ready method.
Alternatively, you could call constants.reload() on initialization and hang all possibly-dependent operations until it completes. This approach would also obsolete the ready method.
What can I do to figure out what's wrong?
You could have analyzed your logs and observed that "Loaded X countries" and "Loaded Y carriers" were sometimes written after "Unknown carrier", helping you realize that the success of utils.getCarrierByHandle() was a race condition.
I am trying write a restapi using express framework and node.js. I am facing an error which I am unable to find out the root cause. I am getting the following error while trying to execute the code :
TypeError: Cannot read property 'node_type' of undefined where 'node_type' is a value that comes from a function
var GdbProcess = require('../../dao/gdb/processnds')
var mongo = require('mongodb');
var async = require('async');
exports.executeService = function(req,res){
//Make the process object to query
var manualProcessQuery = new Object();
manualProcessQuery.index = req.params.processmap;
manualProcessQuery.key = "pid";
manualProcessQuery.value = req.params.pid;
manualProcessQuery.event = req.params.event;
var tempDataNodeToExecute = new Object();
//This function returns an object (dataNodeToExecute) to execute
GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute){
if(err) res.send(err);
tempDataNodeToExecute = dataNodeToExecute;
var isSystem = false;
if (tempDataNodeToExecute.node_type =="system"){
isSystem = true;
}
var count = 0;
async.whilst(
function () { return isSystem },
function (callback) {
//execute the function
executeSystem(dataNodeToExecute,function(err,executionStatus){
if (err) callback(err);
count++;
if(executionStatus=="completed"){
manualProcessQuery.value = tempDataNodeToExecute.pid;
manualProcessQuery.event = "completed";
GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute2){
if(err) callback(err);
tempDataNodeToExecute = dataNodeToExecute2;
if (tempDataNodeToExecute.node_type == "manual"){
isSystem = false;
}
});
callback();
}
});
},
function (err) {
if(err) res.send(err);
res.send("success");
}
);
});
}
var executeManual = function(prosNodeToExecute,callback){
//do something
callback (null);
}
var executeSystem = function(prosNodeToExecute,callback){
//do something
callback(null,"completed");
}
When I debug the code, i clearly see that node_type is available. Can someone help me to find the root problem here ?
remove the new object tempDataNodeToExecute and use dataNodeToExecute instead of it, and it is a good practice to check for null of an object before using its property so that the program does not crashes.