AWS Lambda-Nodejs函数将不会返回数据 [英] AWS Lambda - Nodejs function will not return data

查看:104
本文介绍了AWS Lambda-Nodejs函数将不会返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NodeJS函数调用的新手,并且已经在屏幕上敲了几个小时,而我所有的谷歌搜索都无济于事.

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.

因此,我拥有的是一个AWS Lambda函数,该函数接收具有单个ID号的JSON对象.传递此ID号,并最终将其作为myid发送到getJson函数中.该部分正在运行,它使用NPM的REQUEST模块,并且可以访问Web服务并提取数据.当我console.log(body)时,我看到了我需要的JSON对象.

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.

问题是我无法让它返回数据,所以我可以在其他地方使用JSON.我已经尝试了CALLBACK(身体),RETURN(身体),但没有任何东西能使我获得要使用的数据.

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.

我尝试在函数中使用回调,它确实按应有的方式调用了该函数,但由于某种原因,即使该函数也不会返回供我使用的数据.我已经将JSON硬编码为一个变量,然后将其返回并可以正常工作...但是,如果我使用REQUEST,它将不会把它还给我.

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);
}

推荐答案

一旦您在JavaScript中调用了以回调为参数的函数,就无法通过返回从回调中获取值,因为该函数异步执行.为了从回调中获取值,该回调必须最终调用lambda函数回调函数.

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.

在您的情况下,函数"returningData"需要调用lambda回调函数.

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

这将是以下结构:

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);
};

这篇关于AWS Lambda-Nodejs函数将不会返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆