2

I have a problem with my little Node.js test setup. I basically want an endpoint that I can call, and this endpoint can call different other endpoints and give me a JSON as a response. When I have a look at the console output of the performRequest function, everything looks good. But the return of this function doesn't get passed. I always get an empty {} as a response.

The routes.js that holds my routes:

var s24 = require("./s24");

var routes = function(app) {

  app.get("/ping", function(req, res) {
    res.send("<p>pong</p>");
    console.log("Received GET");
  });

  app.get("/getCategories", function(req, res) {
    var output = s24.getCategories();
    res.type('application/json');
    return res.send(output);
  });

};

module.exports = routes;

The s24.js that queries another REST-API:

var functions = require('./functions');

var appID = "XYZ";
var authorization = "ABC";

var getCategories = function () {
  var output = functions.performRequest("https://api.s24.com/v3/"+appID+"/categories", authorization);
  console.log(output);
  return output;
};

module.exports.getCategories = getCategories;

The functions.js that holds all my relevant functions:

var unirest = require('unirest');

var performRequest = function(endpoint,authorization,body) {
  unirest.get(endpoint)
  .headers({'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': authorization})
  .send(body)
  .end(function (response) {
  var data = response.body;
  console.log(data);
  return data;
  });
};

module.exports.performRequest = performRequest;
Tom Meyer
  • 31
  • 3
  • 1
    Duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Marcos Casagrande May 01 '17 at 19:29
  • Can you show me how I can accomplish this within my code? What part do I need to change? – Tom Meyer May 01 '17 at 19:40
  • 2
    the function performRequest should return a promise, and then use `performRequest.then(response => console.log(response))` – Marcos Casagrande May 01 '17 at 19:43

1 Answers1

0

performRequest is an asynchronous function usually in node.js you can't just return the response of an asynchronous function. You can use async await feature with babel but the simplest way is just to use callbacks or promises. Your code should look something as following:

var performRequest = function (endpoint, authorization, body, callback) {
  unirest.get(endpoint)
    .headers({
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': authorization
    })
    .send(body)
    .end(function (err, response) {
      var data = response.body;
      callback(data["id"]);
    });
}


var getCategories = function () {
  var output = functions.performRequest(
    "https://api.s24.com/v3/" + appID + "/categories",
    authorization,
    function (output) {
      console.log(output);
    });
};

As you can see passing the response from one callback to an other is seems ugly so I suggest you to rethink your design.

Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53