javascript - 将 DynamoBD 与 API Gateway 集成的问题
问题描述
我正在尝试将 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)"
]
}
解决方案
推荐阅读
- python - 函数语句中的名称绑定
- arrays - 使用 Firebase 实时数据库向用户发送表单
- spring-boot - 调用 API 时如何通知订阅者 API?
- prometheus - 何时使用 sum_over_time 与增加 Promql Grafana
- python - 删除行时如何检查多个数据
- python-3.x - 如何使用 distutils 将 C 扩展构建为 C 扩展主模块的子模块?
- r - 将引用的参数传递给容器的 azure webapp --startup-file
- mobile - 您可以在 Edge 移动应用程序中使用 devtools 吗?
- laravel - 在 laravel 中保存多个外键
- mongodb - 如何从docker连接到mongodb?