1

I am slamming right into a Node JS learning curve here. I have an application in Node JS Express using request to make a POST to another API. The call goes through fine, and my console log shows the correct data coming back. My problem is how to get that response out of the request.post(...) and use it in the method which is making the request.

Here's my scenario. An external application makes a call to my API. My API has to call another API to get some data to check for updates. (I have an API making a POST request in order to respond to a POST request from the external app.)

This is the method in my API which makes the request to the third party for some data. I need to get the data from this POST response, do something with it before returning it in my response to the external app's POST request.

exports.getUpdates = function(variable1, variable2, variable3) {
     request.post(
        'http://myurl.exp/api//getUpdates',
        {json: {var1: variable1, ...}},
        function (error, response, body) {
           if(!error && response.statusCode == 200) {
             console.log(body);
           } else {console.log(error);}
        }
     );
  <I need to have this method return the response to the controller that called this method>
 };

I have seen a LOT of examples, but they all just say console.log() which I'm growing to hate.I'm guessing it has to do with callbacks and how I'm not handling it correctly, but none of my research has shown me a clear way to deal with the callback. Any help is appreciated.

RBradley49
  • 13
  • 1
  • 3
  • Possible 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) – peteb Feb 09 '17 at 04:21

1 Answers1

1

Make use of callbacks

exports.getUpdates = function(variable1, variable2, variable3, callback) {
     request.post(
        'http://myurl.exp/api//getUpdates',
        {json: {var1: variable1, ...}},
        function (error, response, body) {
           if(!error && response.statusCode == 200) {
             callback(error, response, body);
           } else {console.log(error);}
        }
     );
 };

Now you can pass a callback while calling this function:

getUpdates(var1, var2, var3, function(error, response, body) {
    //stuff that you want to perform after you get response or error
});

However, I suggest cleaner way to do this:

exports.getUpdates = function(variable1, variable2, variable3, callback) {
     request.post('http://myurl.exp/api//getUpdates', 
                     {json: {var1: variable1, ...}}, callback);
 };

Now you can pass a callback while calling this function:

getUpdates(var1, var2, var3, function(error, response, body) {
        if(!error && response.statusCode == 200) {
            // stuff you want to do
        } else {
            console.log(error);
        }
    }
});
Sandesh K
  • 919
  • 5
  • 10
  • Thank you, that was exactly what I needed, it worked beautifully. What I ended up with was what you showed with an assignment of the callback body to the original response. – RBradley49 Feb 09 '17 at 05:52
  • In the original request route handler: exports.getUpdates = function(req, res) { event.getUpdates( req.body.var1, req.body.var2, req.body.var3, function(error, response, body) { if(!error && response.statusCode == 200) { res.json(body); } else { console.log(error); } } }; – RBradley49 Feb 09 '17 at 06:07
  • In event controller: exports.getUpdates = function(variable1, variable2, variable3, callback) { request.post('http://myurl.exp/api//getUpdates', {json: {var1: variable1, ...}}, callback); }; This returned the body JSON to the external app which shows it is working, now I can get the real code in place. Thanks Sandesh! – RBradley49 Feb 09 '17 at 06:07