我正在尝试将 dynamoDb 与 API Gateway 一起使用,但它不起作用。这就是我想要做的
export async function handler(event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
switch (event.httpMethod) {
case "GET":
var params = {
TableName: 'Users',
Key: {
UserID: event.queryStringParameters.UserID
}
};
dynamodb.get(params, function (err, data) {
if (err){
return{
statusCode: 200,
body: JSON.stringify({
message: "Item not found"
})
}
} else {
return {
statusCode: 200,
body: JSON.stringify({
message: data.Item,
})
};
}
})
break;
}
每次我尝试调用我的网关时,我都会得到一个
{
"message": "Internal server error"
}
我的集成有问题吗?
另一个疑问,如何将其他路由添加到我的网关?我正在使用 CloudFloration 模板,它是这样的:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
UserAPI:
Type: AWS::Serverless::Function
Properties:
Handler: build/userAPI.handler
Runtime: nodejs14.x
Events:
Api:
Type: Api
Properties:
Path: /users
Method: ANY
如何添加 /user (GET) 或 /users (POST) 之类的路由?
根据要求,我得到了日志,所以没有调用 lambda 我得到的错误是:
2021-06-23T12:08:37.557Z eb945ca9-a4b6-4f8e-add1-774276db2cb7 ERROR Invoke Error {
"errorType": "TypeError",
"errorMessage": "Cannot read property 'UserID' of null",
"stack": [
"TypeError: Cannot read property 'UserID' of null",
" at Runtime.handler (/var/task/build/userAPI.js:12:57)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}