gpt4 book ai didi

amazon-web-services - 无法获取 TableName 无服务器框架 : Missing required key 'TableName' in params

转载 作者:行者123 更新时间:2023-12-05 02:41:34 28 4
gpt4 key购买 nike

我关注了这个YouTube tutorial首先尝试使用无服务器框架开发无服务器 rest api。它给出了 MissingRequiredParameter 的错误并将初始化的表名称设置为未定义。

为了缓解这种情况,我在 GitHub 上找到了类似的代码但是部署它也会出现同样的错误。

以下是云 watch 中的控制台日志:

2021-06-19T23:12:12.606Z    97a3c3b8-375b-4e12-9b89-7ad1e44f1873    INFO    Error saving the kitten to the DB:  MissingRequiredParameter: Missing required key 'TableName' in params
at ParamValidator.fail (/var/task/node_modules/aws-sdk/lib/param_validator.js:50:37)
at ParamValidator.validateStructure (/var/task/node_modules/aws-sdk/lib/param_validator.js:61:14)
at ParamValidator.validateMember (/var/task/node_modules/aws-sdk/lib/param_validator.js:88:21)
at ParamValidator.validate (/var/task/node_modules/aws-sdk/lib/param_validator.js:34:10)
at Request.VALIDATE_PARAMETERS (/var/task/node_modules/aws-sdk/lib/event_listeners.js:132:42)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at callNextListener (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
at /var/task/node_modules/aws-sdk/lib/event_listeners.js:86:9
at finish (/var/task/node_modules/aws-sdk/lib/config.js:386:7)
at /var/task/node_modules/aws-sdk/lib/config.js:404:9 {
code: 'MissingRequiredParameter',
time: 2021-06-19T23:12:12.603Z
}



2021-06-19T23:12:12.606Z 97a3c3b8-375b-4e12-9b89-7ad1e44f1873 DEBUG putParams: { TableName: undefined, Item: { name: 'Fluffy', age: 4 } }


它给出错误 502 Bad Gateway

serverless.yaml 文件

service: lambda-kittens-app

provider:
name: aws
runtime: nodejs12.x

iamRoleStatements:
- Effect: 'Allow'
Action:
- dynamodb:PutItem
- dynamodb:Scan*
- dynamodb:GetItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/${self:service}-kittens-${opt:stage}

environment:
DYNAMODB_KITTEN_TABLE: ${self:service}-kittens-${opt:stage}
functions:
create:
handler: handler.createKitten
events:
- http:
path: /v1/kitten
method: post
list:
handler: handler.listKitten
events:
- http:
path: /v1/kitten
method: get
get:
handler: handler.getKitten
events:
- http:
path: /v1/kitten/{name}
method: get
update:
handler: handler.updateKitten
events:
- http:
path: /v1/kitten/{name}
method: patch
delete:
handler: handler.deleteKitten
events:
- http:
path: /v1/kitten/{name}
method: delete
resources:
Resources:
kittensTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: ${self:service}-kittens-${opt:stage}
AttributeDefinitions:
- AttributeName: name
AttributeType: S
KeySchema:
- AttributeName: name
KeyType: HASH
BillingMode: PAY_PER_REQUEST
Plugins:
- serverless-pseudo-parameters
# Outputs:
# NewOutput:
# Description: "Description for the output"
# Value: "Some output value"

对 db-handler.js 所做的更改

AWS.config.update({ region: 'us-east-1' });

感谢您的帮助。谢谢!

注意:我确实检查了几个解决方案,但它们似乎都不起作用。

最佳答案

没有看到您的应用程序代码,我无法确定。但是我认为你正在做这样的事情:

const documentClient = new DynamoDB.DocumentClient({ params: { TableName: process.env.DYNAMODB_KITTEN_TABLE } });

... // Put call

我注意到在您的 serverless.yml 中,environment: block 位于根级别。那行不通,因此您的环境变量未定义/未设置。

environment block 位于 provider block 内,或位于 function block 内(取决于您是否要为每个设置环境变量函数,或堆栈中的所有函数):

provider:
name: aws
runtime: nodejs12.x
environment:
DYNAMODB_KITTEN_TABLE: ${self:service}-kittens-${opt:stage}

关于amazon-web-services - 无法获取 TableName 无服务器框架 : Missing required key 'TableName' in params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68051340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com