Tag: node.js

How to avoid multiple node processes doing repetitive things?

I have a module in Node.js which repeatedly pick a document from MongoDB and process it. One document should be processed only once. I also want to use multiple processes concept. I want to run the same module(process) on different processors, which run independently. The problem is, there might be a scenario where the same […]

Persist cookies across NightmareJS instances

How would I persist and pass cookies along multiple NightmareJS instances? Any example code would be helpful.

Error : Callback was already called when using pg-promise with async series

I’m having trouble understanding the output printed why executing this code : 1 2 Unhandled rejection Error: Callback was already called. It seems like both then and catch are executed when the query is successful. Any idea ? Cheers async.series([ function(callback) { db.none(query) .then(function () { return callback(null, true); }) .catch(function (err) { return callback(err, […]

Catching errors from spawned Node.js process

Here is an example where child process error is not fired: const spawn = require(‘child_process’).spawn; const childProcess = spawn(‘tar’, [‘–wrong-option’], { stdio: ‘inherit’ }); childProcess.on(‘error’, err => { console.error(‘err: ‘, err); }); Why is it so? How can the error (error code in particular) be caught from spawned process?

AWS Lambda not returning list of alarms

I have a small node.js lambda in AWS (see code below). When it runs, it reports “success”, but does not print a list of CloudWatch alarms as I would expect it to. The AWS Javascript SDK leads me to believe what I’m doing should work. We do have a number of alarms setup, so I’m […]

Check condition from backend (if-else) without eval

I have node script and backend, the script contains certain data and it got some conditions from backend. For ex node script: var data={ count: 10, length: 27, days: 3 }; var condition = ‘count > 10 && length < 3’; // <=== this condition got from backend if( … condition …) { // action […]

Pipelining setTimeout in node.js

I am writing a node.js console application. I have two setTimeout functions as follows. setTimeout(function(){ process.stdout.write(‘\n\nJokes Apart !!!\n\n’); }, 2000); setTimeout(function(){ process.stdout.write(‘\n\nLet\’s play a game !!!\n\n’); }, 1000); What I actually intend is, the first message should come after 2 seconds. After that, the second message should appear after 1 second. But what’s happening is, […]

javascript : make a new safe class constructor

sometimes we loss the new keyword when define new object, obj = new Clazz(); //correct obj = Clazz(); //wrong, but no syntax error, hard to debug. I want to write a function to help me create Class and make it new safe. var Class = function(){ var constructor = arguments[0]; var superClasses = arguments[1..n]; function […]

What is the scope of javascript callback and anonymous functions?

I have written the following code using node.js and riak-js. I have a recursive function walk that should be a list of JSON documents, but instead returns an empty list… why? how to fix? require(‘riak-js’); var walk = function(bucket, key, list){ if(list == undefined){ var list = new Array(); } db.get(bucket, key)(function(doc, meta){ list.push(doc); if(meta.links.length […]

Node.js modules vs Objects

I have been playing around with node.js, and coming from a Java background, I am struggling to differentiate between modules and the typical concept of objects in JavaScript. When implementing a modules, I am currently doing it this way: // someModule.js var privateVariable; this.sampleFunction = function() { // … } Now, the way I am […]