json - NodeJS 请求在 AWS Lambda 中未给出任何响应

标签 json node.js amazon-web-services aws-lambda

我正在使用 NodeJS 请求模块将 JSON 请求传递到 URL 并从中生成 JSON 响应。我尝试了这段代码,它生成了有效的响应。我粘贴了我提出的相同 StackOverflow 问题的链接。

NodeJS Request returning an empty array inside a JSON response

但是,当我在 AWS Lambda 中使用相同的逻辑时,模块根本没有任何响应。由于根本没有响应,我无法理解问题所在。

这是以 Alexa 作为触发器的 AWS Lambda 的处理函数。

'use strict';

var request = require('request');

var accountNumberRequest = {};
var balanceResponse = {};
const url = "https://ibluatapig.indusind.com/app/uat/balinq/AccountEnquiry?client_id=6867b781-9b21-45c5-9c55-948f7cd1a33f&client_secret=hP3yB3hM2oH4pH4hM1kV3uY8vR3qV7jY8cF6bG2sF5jX8lT1vN";
var bal = {};
exports.handler = function (event,context) {
    try{
        console.log("Try Started");
        var req = event.request;
        console.log("Request Generated");
        if(req.type === "LaunchRequest") {
            console.log("Launch Request! Calling handleLaunchRequest");
            handleLaunchRequest(context);
        } else if(req.type === "IntentRequest") {
            console.log("IntentRequest");
            let options = {};
            console.log(0);
            if(req.intent.name === "BalanceIntent") {
            console.log("Balance Intent");
                //Got the account number from Alexa request
                let accNo = req.intent.slots.AccountNumber.value;
                console.log(accNo);
                accountNumberRequest = {
                    "AERequest":{
                        "serviceType":"BE",
                        "deviceId":"Test",
                        "accountId":accNo
                        }
                };
                console.log(accountNumberRequest);
                console.log("Calling NodeJS.Request");
                request({
                    url: url,
                    method: "POST",
                    json: true,
                    header: {
                        "content-type": "application/json",
                    },
                    body: accountNumberRequest
                    }, 
                    function(error, response,body){
                        if(!error && response.statusCode === 200){
                            console.log(body.AEResponse.AcctBal[1].BalAmt);
                        } else {
                            //options.speechText = `The account <say-as interepret-as = "digits">${accNo}</say-as> does not exist`;
                            console.log("error: "+error);
                            console.log("response.statusCode"+response.statusCode);
                            console.log("response.statusText"+response.statusText);
                        }
                    }
                );
                console.log("Balance Response should be assigned by now");
                console.log(bal);


                /* if(accountNumbers.hasOwnProperty(accNo)) {
                    var balance = accountNumbers[accNo];
                    accountExists = true;
                }
                if(accountExists == true){
                    options.speechText = `The balance of account number <say-as interpret-as = "digits">${accNo}</say-as> is <say-as interpret-as = "cardinal">${balance}</say-as>`;
                } else {
                    options.speechText = `The account <say-as interepret-as = "digits">${accNo}</say-as> does not exist`;
                }*/
                context.succeed(buildResponse(options));
            }
        } else if(req.type === "SessionEndedRequest") {
            //Code here
        } else {
            throw("Unknown Intent Type");
        }

    } catch(e){
        context.fail("Exception "+e);
    }
};
function getBalance(){
    //Code to parse the JSON response and extract values from the response.
    }

function handleLaunchRequest(context){
//Code for handling launch requests    }

function buildResponse(options){
    //Code for generating response
}

最佳答案

这就是问题所在...

// You're sending an asynchronous HTTP request here.
request();
// But you sent the response here without waiting for the above request to finish.
context.succeed();

基本上,您在 request() 完成之前执行 context.succeed()。因此,您基本上是在没有来自该 HTTP 请求的响应的情况下结束 Lambda 调用。

要修复您的代码,请将 context.succeed() 内部放入您传递给 request() 调用的回调中。

附注

您应该使用callback,而不是已弃用的context.succeed()/context.fail() API。

关于json - NodeJS 请求在 AWS Lambda 中未给出任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379925/

相关文章:

python - 将 JSON 文件转换为 Pandas 数据帧

javascript - 使用 JSON 制作 XmlHttpRequest POST

Python JSON 添加键值对

ios - AWSMobileHub 使用 Swift 在 IOS 中缓存来自 S3 的图像

amazon-web-services - 删除 AWS 中的自动数据库快照

amazon-web-services - amazon cloudfront 和amazon s3 传输加速有什么区别吗?

php - 使用 PHP 获取 &lt;script type ="application/ld+json"> 的内容

javascript - 如何在nodejs中先实现mysql查询? (或者先实现条件查询)

node.js - 将 Nodejs 与 IIS 结合使用,以使用 Express 实现 REST 服务

node.js - 找不到模块 'browserify'