对于以下使用 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/