验证Exception一个或多个参数值无效:丢失项目中的键prientar_key
我正在使用无服务器来公开getWayAPI,lambda函数和dynamoDB中的HTTP请求,但出现了一个错误“验证Exception一个或多个参数值无效:丢失项目中的键prientar_key”。请帮我!
在这里,我的代码在createCustomer.js文件中
'use strick'
const AWS = require('aws-sdk')
const { tableName } = require('./config');
module.exports.createCustomer = async(event) => {
const body = JSON.parse(JSON.stringify(Buffer.from(event.body, 'base64').toString()))
const dynamoDb = new AWS.DynamoDB.DocumentClient()
const putParams = {
TableName: tableName,
Item: {
primary_key: body.name,
email: body.email
}
}
await dynamoDb.put(putParams).promise()
return {
statusCode: 201
}
}
,这是我的serverless.yml文件代码
org: surangi
app: my-api
service: my-api
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs14.x
environment:
DYNAMODB_CUSTOMER_TABLE: ${self:service}-customerTable-${sls:stage}
iam:
role:
statements:
- Effect: 'Allow'
Action:
- 'dynamodb:PutItem'
- 'dynamodb:Get*'
- 'dynamodb:Scan*'
- 'dynamodb:UpdateItem'
- 'dynamodb:DeleteItem'
Resource: arn:aws:dynamodb:${aws:region}:${aws:accountId}:table/${self:service}-customerTable-${sls:stage}
functions:
createCustomer:
handler: createCustomer.createCustomer
events:
- httpApi:
path: /
method: post
resources:
Resources:
CustomerTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: primary_key
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: primary_key
KeyType: HASH
TableName: ${self:service}-customerTable-${sls:stage}
我的.env文件具有此变量: dynamodb_customer_table:“ my-api-customertable-dev”
现在我遇到了一个错误,例如:
node_modules/aws-sdk/lib/lib/stoloption/json.js 52 resp.Error = util.error(new Error(),错误); ^ validationException一个或多个参数值无效:缺少项目中的键prientar_key
I'm using serverless to expose HTTP request in getwayapi, lambda function and dynamodb but got an error "ValidationException One or more parameter values were invalid: Missing the key primary_key in the item". Please help me!
Here my code in createCustomer.js file
'use strick'
const AWS = require('aws-sdk')
const { tableName } = require('./config');
module.exports.createCustomer = async(event) => {
const body = JSON.parse(JSON.stringify(Buffer.from(event.body, 'base64').toString()))
const dynamoDb = new AWS.DynamoDB.DocumentClient()
const putParams = {
TableName: tableName,
Item: {
primary_key: body.name,
email: body.email
}
}
await dynamoDb.put(putParams).promise()
return {
statusCode: 201
}
}
and here is my serverless.yml file code
org: surangi
app: my-api
service: my-api
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs14.x
environment:
DYNAMODB_CUSTOMER_TABLE: ${self:service}-customerTable-${sls:stage}
iam:
role:
statements:
- Effect: 'Allow'
Action:
- 'dynamodb:PutItem'
- 'dynamodb:Get*'
- 'dynamodb:Scan*'
- 'dynamodb:UpdateItem'
- 'dynamodb:DeleteItem'
Resource: arn:aws:dynamodb:${aws:region}:${aws:accountId}:table/${self:service}-customerTable-${sls:stage}
functions:
createCustomer:
handler: createCustomer.createCustomer
events:
- httpApi:
path: /
method: post
resources:
Resources:
CustomerTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: primary_key
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: primary_key
KeyType: HASH
TableName: ${self:service}-customerTable-${sls:stage}
my .env file has this variable:
DYNAMODB_CUSTOMER_TABLE: "my-api-customerTable-dev"
now i am getting an error as:
node_modules/aws-sdk/lib/protocol/json.js
52
resp.error = util.error(new Error(), error);
^ ValidationException One or more parameter values were invalid: Missing the key primary_key in the item
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用Console.LOG语句打印变量的值。在我看来,身体可能没有正确提取,因此主钥匙没有值。如果您使用的话,您可以在CloudWatch或无服务器框架仪表板中的日志中查看console.log的值。
I would recommend using a console.log statement to print out the value of variables. It looks to me as if the body is probably not being extracted correctly and so there is no value for the primary key. You can see the value of the variable you console.log in your logs in CloudWatch or in the Serverless Framework dashboard if you use it.