Nodejs API call returning undefined to lambda function

做~自己de王妃 提交于 2020-01-23 08:36:44

问题


This is the aws lambda function which will invoke an api:

'use strict';

var request = require("request")

exports.handler = function (event, context,callback) {



let url = "https://3sawt0jvzf.execute-api.us-east-1.amazonaws.com/prod/test"

request({
    url: url,
    method: "POST",
    json: event,

}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        callback(null, { "isBase64Encoded": true|false,
                          "statusCode": "200",
                          "headers": { "headerName": "headerValue"},
                          "body": body});
    }
    else {

        console.log("error: " + error)
        console.log("response.statusCode: " + response.statusCode)
        console.log("response.statusText: " + response.statusText)
    }
})
};

This is the api written as an aws lambda function:

'use strict';


exports.handler = function(event, context, callback) {
console.log(event.name);
callback(null, { "isBase64Encoded": true|false,
                 "statusCode": "200",
                 "headers": { "headerName": "headerValue"},
                 "body": `Hello World ${event.name}`});  // SUCCESS with message
};

When I try to call the api from the lambda function it just returns "Hello World undefined". It is not appending the name at the end and returning the correct response.


回答1:


Assumptions:

  • You're using Lambda-Proxy Integration.
  • You want to pass the the exact same payload that the first Lambda received to the second Lambda.*

You're misunderstanding what event is. This is NOT the JSON payload that you sent through your HTTP request.

An HTTP request through the API Gateway gets transformed into an event object similar to this:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

As you can see, the JSON payload is accessible in a stringified form in event.body.

If you want to send the pass the same payload to the second Lambda, you have to parse it first.

const body = JSON.parse(event.body)

Then, send body instead of event.

Then, in your second Lambda, you parse the stringified JSON in event.body and then you get your original payload back.

If you sent name in that original payload, you can get it from JSON.parse(event.body).name.

Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format




回答2:


Had a similar problem and debugged with logging event to console.

Add logging on the event,

console.log(JSON.stringify(event));

to evaluate how mapping is done in your API-Gateway to Lambda integration and see where the post parameter exists.

If the post value is not there fix the integration until you get the post values in your event.

Data Mapping API-Gateway to Lambda:

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

Hope it helps.



来源:https://stackoverflow.com/questions/46425573/nodejs-api-call-returning-undefined-to-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!