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.