0

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?

Jay
  • 496
  • 1
  • 4
  • 11
  • @cybersam It looks like you commented rather than voting to close. Is there a reason you did this? (There were no close votes when I first saw your comment) – Aaron Dufour May 22 '14 at 02:37
  • not sure it's the same issue but I do see how I might try using the callback as mentioned in the other post. Just curious if anyone has a better idea. – Jay May 22 '14 at 02:43

0 Answers0