Node.js callback is not a function - node.js

Running a simple redis test
var redis = require(__dirname+'/server/redis.js');
redis.redisTest(function(res){
RedisState = res;
});
redis.js
redisTest: function(callback){
GameDBCon.set('my test key', 'my test value');
GameDBCon.get('my test key', function (error, result) {
if (error) {
callback(false);
console.log(error);
throw error;
}
console.log('GET result ->' + result);
callback(true);
});
},
error from redis.js at callback(true);
callback is not a function, I see a lot of similar issues like this but its not making much sense. Additionally I have similar code that does work.
Shouldn't this callback work?

Related

functions in functions nodejs

I want to get the klout score using a screen name (twitter).
I did this. I know it doesn't work like that, but I don't know how it should work.
function get_klout(screenName){
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
return Math.round(klout_response.score);
});
});
}
I want my function to return this number Math.round(klout_response.score);
function get_klout(screenName) {
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
return Math.round(klout_response.score);
});
});
}
Your function is async, so you can't just assign what it returns to a variable because you will just assign undefined:
var result = get_klout('foo'); // undefined
what you can do is:
using async functions in node 8+
using Promises
using callbacks:
function get_klout(screenName, done) {
klout.getKloutIdentity(screenName, function(error, klout_user) {
klout.getUserScore(klout_user.id, function(error, klout_response) {
done(Math.round(klout_response.score));
});
});
}
get_klout('foo', function(response) {
console.log(response);
});
just a note:
In node is a common pattern implementing the error first callback and you should have a look on it because it's the traditional and more used way to handle errors:
http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/
Because this is an asynchronous function you cannot use return, in order to return result from asynchronous code pass to your function callback and return the result on the callback. you should handle errors in your callbacks.
function get_klout(screenName, callback){
klout.getKloutIdentity(screenName, function(error, klout_user) {
if (err){
callback(error);
return
}
klout.getUserScore(klout_user.id, function(error, klout_response) {
if (err){
callback(error);
return
}
callback(null, klout_response.score);
});
});
}
get_klout(screenName, function(err, res){
if (err){
console.log(err);
return
}
console.log(res);
});

Jasmine Testing Node.js Async modules

I\m trying to write unit tests for some code I wrote, the problem I'm running into is I expect my mock callback to be called after executing the function but my test fails as it is never called.
describe("Asynchronous specs", function() {
var mockNext;
beforeEach(function() {
mockNext = jasmine.createSpy('mockNext');
var res;
parallelRequests.APICall(testObject[0], null, mockNext);
});
it("callback spy should be called", function () {
expect(mockNext).toHaveBeenCalled();
});
});
The function being tested is quite simple:
function APICall(options, res, next) {
request(options, callback);
function callback(error, response, body) {
if (error) {
if (error.code === 'ETIMEDOUT') {
return logger.error('request timed out: ', error);
next(error);
}
logger.error('request failed: ', error);
next(error);
}
next(null);
}
}
The issue I suspect is jasmine testing the expectation before the mock callback is executed in API Call due to request's async nature. I've tried using what others suggest of using done() and flags but with no luck. Would appreciate some guidance in this matter.
Your beforeEach code is asynchronous. You have to tell yasmin when your beforeEach logic is done. You can solve this by the callback method done, that is passed to each test. Try this:
describe("Asynchronous specs", function() {
var mockNext;
beforeEach(function(done) {
parallelRequests.APICall(testObject[0], null, function(){
mockNext = jasmine.createSpy('mockNext');
mockNext();
done();
});
});
it("callback spy should be called", function () {
expect(mockNext).toHaveBeenCalled();
});
});

Rethinkdb with nodejs and expresso

I am trying to use rethinkdb and test it via expresso. I have function
module.exports.setup = function() {
var deferred = Q.defer();
r.connect({host: dbConfig.host, port: dbConfig.port }, function (err, connection) {
if (err) return deferred.reject(err);
else deferred.resolve();
});
return deferred.promise;
});
I am testing it like this
module.exports = {
'setup()': function() {
console.log("in setup rethink");
db.setup().then(function(){
console.log(clc.green("Sucsessfully connected to db!"));
}).catch(function(err){
console.log('error');
assert.isNotNull(err, "error");
});
}
};
And I am runing code like this
expresso db.test.js
But expresso shows error 100% 1 tests even in case of error.
I tried to put throw err; in catch, but nothing changes.
But if I put assert.eql(1, 2, "error"); in the begining of setup() it fails as expected;
Is there something, that caches errors? How can I make it fail as it should be?
For squalize I found
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) {
throw e;
});
Is there something like this for rethink db?
The problem is that this test is asynchronous, and you're treating it as a synchronous test. You need to do the following:
module.exports = {
'setup()': function(beforeExit, assert) {
var success;
db.setup().then(function(){
success = true;
}).catch(function(err){
success = false;
assert.isNotNull(err, "error");
});
beforeExit(function() {
assert.isNotNull(undefined, 'Ensure it has waited for the callback');
});
}
};
Mocha vs Express
You should consider taking a look at mocha.js, which has a much nicer API for asynchronous operations by passing the done function. The same test would look like this:
module.exports = {
'setup()': function(done) {
db.setup().then(function(){
assert.ok(true);
}).catch(function(err){
assert.isNotNull(err, "error");
})
.then(function () {
done();
});
}
};
Promises
The first function you wrote can be re-written in the following manner, because the RethinkDB driver, by default, returns a promise on all operations.
module.exports.setup = function() {
return r.connect({host: dbConfig.host, port: dbConfig.port });
});

Callback not getting called while using async.series

I have the following code, where i am calling a callback after my async task is completed:
var async = require("async");
function _callback(err, result){
if(err) console.log('Error Occurred');
console.log('Callback called');
console.dir(result);
}
function tasks() {
console.log('Start executing tasks');
var tasks = [];
var result = {};
tasks.push(function(_callback) {
console.log('Getting some data');
_callback(null, result);
});
tasks.push(function(_callback) {
console.log('Second function called');
_callback(null, result);
});
async.series(tasks, function(){
console.log('All done');
});
}
tasks();
I have checked against the syntax expected in the async library. Code looks similar to me.
Can someone point out whats needs to be changed here.
Try it like this:
var async = require("async");
function _callback(err, result){
if(err) console.log('Error Occurred');
console.log('Callback called');
console.dir(result);
}
function tasks() {
console.log('Start executing tasks');
var tasks = [];
var result = {};
tasks.push(function(next) {
console.log('Getting some data');
next(null, result);
});
tasks.push(function(_callback) {
console.log('Second function called');
next(null, result);
});
async.series(tasks, function(err, result){
console.log('All done');
_callback(err, result);
});
}
tasks();
Async gives every single task a callback which you need to call to let async now that this task finished.
After all tasks are finished you can call your callback.
If you want your callback to get called after every single task is finished just add before every
next(null, result) also an _callback(null, result)
The reason that wouldn't work on your implementation is that you overwrite the global _callback method with the argument that has the same name.
Hope that makes sense :)
As it stands in your code right now, the _callback function you define above should never get called, as the _callback in the tasks functions is scope to the one passed by async.
// this will never get called
function _callback(err, result){
if(err) console.log('Error Occurred');
console.log('Callback called');
console.dir(result);
}
function tasks() {
console.log('Start executing tasks');
var tasks = [];
tasks.push(function(_callback) { // the _callback here overrides the one above for this closure
console.log('Getting some data');
_callback(null, result);
});
// ... more tasks
async.series(tasks, function(){
console.log('All done');
});
}
If you want to reuse the logic in your _callback function, I'd recommend passing it the async callback as a parameter:
// rename some functions to avoid silly js scoping mysteries
function myCallback(err, result, asyncCallback){
// shared logic here
if(err) console.log('Error Occurred');
console.log('Callback called');
console.dir(result);
asyncCallback(null, result);
}
// ...
tasks.push(function(asyncCallback) { // the _callback here overrides the one above for this closure
console.log('Getting some data');
myCallback(null, result, asyncCallback);
});
// ... more tasks
async wants its callback to be called so it knows when to continue, but that doesn't mean you can't intercept it with your own handlers :P.
You don't need the function call to _callback. You could simplify it like this.
var async = require("async");
function doSomeLogic(item, callback) {
console.log(item);
callback(null, item + 2);
}
function tasks() {
console.log('Start executing tasks');
var tasks = [];
var result = {};
tasks.push(function(callback){
// do some more stuff ...
callback(null, 'one');
});
tasks.push(function(callback){
// do some more stuff ...
callback(null, 'two');
});
async.series(tasks, function(error, results) {
if (error) {
console.log(error);
}
console.log(results);
console.log('All done');
});
}
tasks();

How To Write a Mocha test for async waterfall

I have written a mocha test case for the async waterfall, "function2" is module which call for mongodb to get data, when i am trying to run the same code in node.js this async code works, but when i am do mocha test the "function2" call for mongodb throws error saying "[Error: Cannot determine state of server]".
i got to know the reason that the test is getting executed fast without waiting for mongodb to connect and return result.
can someone suggest me how to solve this problem. thanks in advance.
var function1 = require('../../method1');
var function2 = require('../../method2');
var function3 = require('../../method3');
describe('controller : ', function () {
it('should not return error', function (done) {
async.waterfall([
function(callback){
function1.method1(app.request, app.response, function(err,result) {
if(err){
callback(err,null);
}
else{
var var1 = result;
callback(null, var1);
}
});
},
function(var1, callback){
//Here I have a Mongodb Call
function2.method2(var1,function(err,result) {
if(err) {
callback(err,null);
}
else{
var var2= result;
var context = {
"c1": var1,
"c2": var2
};
callback(null, context);
}
});
},
function(context, callback){
function2.method2(context, function(err,result) {
if(err){
console.error(err);
callback(err,null);
}
else{
context.c3 = {};
result.forEach(function(e, i) {
for(var key in e) {
context.c3[key] = e[key];
}
});
callback(null, context);
}
});
},
function(context, callback){
should.exist(context);
}
],
function (err, result) {
should.not.exist(err);
should.exist(result);
});
});
});
I'm not sure but try to wait when connection (which use your module) is established.
before(function(done) {
connection.on('open', done);
});

Resources