amazon-web-services - AWS Lambda 不从 API 网关读取查询参数

标签 amazon-web-services aws-lambda aws-api-gateway

对于以下使用 URL 使用 Lambda-Proxy 集成的 lambda 函数

/api/random?min=1&max=10

我的代码:

exports.handler = async (event) => {
    let min = event.queryStringParameters.min
    let max = event.queryStringParameters.max
    console.log("min", min)
    console.log("max",max)
    const response = {
        statusCode: 200,
        body: JSON.stringify(Math.floor(Math.random()*max)+min),
    };
    return response;
};

执行失败并出现以下错误:

Response:
{
  "errorType": "TypeError",
  "errorMessage": "Cannot read property 'min' of undefined",
  "trace": [
    "TypeError: Cannot read property 'min' of undefined",
    "    at Runtime.exports.handler (/var/task/index.js:3:43)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:65:25)",
    "    at process._tickCallback (internal/process/next_tick.js:68:7)"
  ]
}

使用 JSON.stringify(event) 记录事件返回正确的参数:

{"min":"1","max":"10"}

最佳答案

Logging the event using JSON.stringify(event) returns the correct parameters:

{"min":"1","max":"10"}

您的 Lambda 事件似乎没有附加 queryStringParameters 属性。您需要将其添加到事件中或将代码修改为:

let { min, max } = event

关于amazon-web-services - AWS Lambda 不从 API 网关读取查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426303/

相关文章:

python - 从未在 AWS Lambda 上找到设置工具

amazon-web-services - 通过使用 amazon api-gateway 的客户端 ssl 身份验证在 EB 环境中进行身份验证

amazon-web-services - 使用 cloudfront 的 s3 上的访问被拒绝错误

apache - 如何将我在 EC2 中的网站重定向到我的域

amazon-web-services - 清除 EB 初始化配置

amazon-web-services - AWS API 网关 : Regex for error is not picked up

amazon-web-services - AWS Lambda 是否加密进行中的请求?

amazon-web-services - 如何使用 Amazon API 网关将 json 数据放入 Kinesis 流

aws-api-gateway - AWS CloudFormation/API Gateway 给出 'Invalid Resource identifier specified'

ruby-on-rails - Heroku + Rails4.2 : Cloudfront setup