NodeJS请求不在AWS Lambda中提供任何响应

时间:2018-01-22 10:55:00

标签: 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中使用相同的逻辑时,模块中根本没有响应。由于根本没有回应,我无法理解问题所在。

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

'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
}

1 个答案:

答案 0 :(得分:3)

这是问题......

// 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();

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

要修复您的代码,请将context.succeed() 置于 内,将您传递的回调转移到request()来电。

P.S。

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