I am trying to understand why the getSalaries variable is logging undefined to the console. The call to the web service is fine and if I replace my return statement in dataResult function with console.log(result); the JSON prints ok. Here is the code:
var http = require('http');
var options = {
host: 'redacted',
port: '80',
path: '/redacted',
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
var getSalaries = function(callback){
var salaries = getMFLData(options, dataResult);
callback(salaries);
}
var processSalaries = function(salaries){
console.log(salaries);
}
getSalaries(processSalaries);
function getMFLData(options, callback){
http.request(options, function(res){
var body = '';
res.on('data', function(chunk){
body+= chunk;
});
res.on('end', function(){
var result = JSON.parse(body);
callback(null, result);
});
res.on('error', callback);
})
.on('error', callback)
.end();
}
function dataResult(err, result){
if(err){
return console.log('an error occured getting MFL data ', err);
}
return result;
};
Thanks for any help on this one. I am simply trying to reuse the getMFL function for multiple calls but cannot figure out how to get the return value.
Dave