Hello: i am new to nodejs and mocha. I trying to use module.exports
to return a value from a callback function. However, its returning undefined
. For simple cases it works though. Please help.
Result
Module Export Example
√ Test Case 1: Module
Hello Node World!!! (*** this works - its a direct return ***)
√ Test Case 2: Module
undefined (*** this fails - its from a callback fn ***)
google.js
var requirejs = require('requirejs');
requirejs.config({ baseUrl: '.', paths: { }, nodeRequire: require });
describe('Module Export Example', function(){
var mod;
before(function(done){
requirejs(['./googleModule'],
function(_mod) {
mod = _mod;
done();
});
});
it('Test Case 1: Module', function(done){
console.log(mod.get(done));
});
it('Test Case 2: Module', function(done){
console.log(mod.google(done));
});
});
googleModule.js
var request = require('request');
module.exports = {
get: function(done){
var a = "Hello Node World!!!";
return(done(), a);
},
google: function(done){
var a = doCallback(function(){
var b = "PRINT DATA: " + data.statusCode + ' ' + data.headers['content-type'];
return(done(), b);
});
return(done(), a);
}
}
function doCallback(callback, done){
var options = {url: 'http://www.google.com', headers: {'Content-Type': 'text/html'}, encoding: null};
request.get(options, function(err, res, body){
var a = callback(res, done);
return (callback(), a); //???????
});
}