I am trying to make a node.js module to retrieve some data from a third part API. I figured it would be cleaner to run the calls into a module and just use that in the applications main.js
When I try and return the value to my app.js it sais undefined which means I can not assign the data to a new var.
Here is my module konny.js (I amusing request module as well)
var request = require('request');
function bResource () {
//byUnique gets by unique ID, Returns assigned values
this.byUnique = function (var1, var2) {
request.get(
{ uri: 'http://mysiteapi/path/'+var1,
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Api-Key': var2
}
},
function callback (error, res, body) {
if(res.statusCode == 200){
return body;
} else {
console.log('error: '+ res.statusCode);
}
}
);
}
}
exports.properties = function() {
return new bResource();
}
and here is my app.js that uses the module:
var konny = require('./konny.js');
var Id = 'MYID';
var key = 'MYKEY';
var foo = konny.properties().byUnique(Id, key);
console.log(foo);
foo comes back undefined. If I console log from the byUnique functions I do see my JSON response?