验证Exception一个或多个参数值无效:丢失项目中的键prientar_key

发布于 2025-02-01 03:25:50 字数 2082 浏览 4 评论 0原文

我正在使用无服务器来公开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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2025-02-08 03:25:50

我建议使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文