Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - Nodejs function will not return data

I'm new to NodeJS function calls and I have been banging my head on the screen for a few hours now and all my googling just hasn't helped.

So what I have is a AWS Lambda function which receives a JSON object, with a single ID number. This ID number is passed along and is eventually sent into the getJson function as myid. This part is working, it's using the REQUEST module from NPM and it reaches out to the web service and pulls back the data. When I console.log(body) I am seeing the JSON object I need.

The problem is I can't get it to RETURN the data back so I can use the JSON somewhere else. I've tried CALLBACK (BODY), RETURN (BODY), but nothing every gives me back the data to use.

I tried using a callback in the function, and it does call that function like it should, but even that function won't return the data for me to use for some reason. I've hard coded the JSON into a variable and returned that and it works... but if I use the REQUEST it just won't give it back to me.

I'm hoping this is something simple... thanks so much in advance!

Calling the function:
            query_result.success = 1;
            query_result.message = "Applicant Data Found";
            query_result.data = getJson(201609260000003, returningData);


function getJson(myid, callback){
    request('http://server.com/service1.asmx/Get_Status_By_External_Id?externalId=' + myid + '',
        function (error, response, body) {
        console.log(body); // I see the JSON results in the console!!!
        callback (body); // Nothing is returned.
        }

    );

}

function returningData(data){
    console.log("ReturningData Function Being Called!");
    var body = '{"Error":null,"Message":null,"Success":true,"ExternalId":"201609260000003","SentTimeStamp":"11/22/2016 1:07:36 PM","RespTimeStamp":"11/22/2016 1:08:32 PM","RespTot":"SRE"}';
    return JSON.parse(body);
}
like image 243
Josh Avatar asked Nov 26 '16 08:11

Josh


1 Answers

Once you have invoked a function in JavaScript that has a callback as an argument, you can not get a value out of the callback by a return, because this function executes asynchronously. In order to get the value from the callback, this callback must invoke the lambda functions callback function eventually.

In your case the function "returningData" needs to call the lambda callback function.

This would be the structure:

exports.lambda = (event, lambdaContext, callback) => { // this is the lambda function

  function returningData(data){
    console.log("ReturningData Function Being Called!");
    var body = '{"Error":null,"Message":null,"Success":true,"ExternalId":"201609260000003","SentTimeStamp":"11/22/2016 1:07:36 PM","RespTimeStamp":"11/22/2016 1:08:32 PM","RespTot":"SRE"}';
    callback(null, JSON.parse(body)); // this "returns" a result from the lambda function
  }

  function getJson(myid, callback2){
    request('http://server.com/service1.asmx/Get_Status_By_External_Id?externalId=' + myid + '', function (error, response, body) {
      console.log(body); // I see the JSON results in the console!!!
      callback2(body); 
    });
  }

  query_result.success = 1;
  query_result.message = "Applicant Data Found";
  query_result.data = getJson(201609260000003, returningData);
};
like image 196
Digitalkapitaen Avatar answered Sep 28 '22 09:09

Digitalkapitaen