【问题标题】:Missing the key id, in the item [serverless framework + lambda + dynamodb]项目中缺少密钥 id [serverless framework + lambda + dynamodb]
【发布时间】:2021-05-18 11:48:24
【问题描述】:

下面是 dynamo-table.yml

Resources:
  AuctionsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: AuctionsTable
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: id,
            AttributeType: S
        KeySchema:
          - AttributeName: id,
            KeyType: HASH

以下是在 dynamodb 中插入记录的处理程序代码。

import AWS from 'aws-sdk';
import { v4 as uuid } from 'uuid';

const dynamodb = new AWS.DynamoDB.DocumentClient();

async function createAuction(event, context) {

  const { title } = JSON.parse(event.body);
  const now = new Date();
  const params = {
    TableName: 'AuctionsTable',
    Item: {
      'id' : {S: `${uuid()}`},
      'title' : {S: `${title}`},
      'status': {S: `OPEN`},
      'createdAt': {S: now.toISOString()}
    }
  };
  console.log(`Auction: ${JSON.stringify(params)}`);
  await dynamodb.put(params).promise();
  return {
    statusCode: 201,
    body: JSON.stringify(params),
  };
}

export const handler = createAuction;

下面是serverless.yml文件的内容,

service:
  name: auction-service

plugins:
  - serverless-bundle
  - serverless-pseudo-parameters

provider:
  name: aws
  runtime: nodejs12.x
  memorySize: 128
  stage: ${opt:stage, 'dev'}
  region: ap-south-1
resources:
  - ${file(src/resources/dynamodb-table.yml)}
  - ${file(src/resources/role.yml)}

functions:
  createAuction:
    handler: src/handlers/createAuction.handler
    role: auctionServiceRole
    events:
      - http:
          method: POST
          path: /auction

custom:
  bundle:
    linting: false
  tableName: "AuctionsTables"

问题:当我点击 POST URL 并访问 CloudWatch 日志时,出现以下错误,

{
    "errorType": "ValidationException",
    "errorMessage": "One or more parameter values were invalid: Missing the key id, in the item",
    "code": "ValidationException",
    "message": "One or more parameter values were invalid: Missing the key id, in the item",
    "time": "2021-02-15T15:02:42.606Z",
    "requestId": "C344O7CTTHF3PAR1OP8BQIFBT7VV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 8.687787589723229,
    "stack": [
        "ValidationException: One or more parameter values were invalid: Missing the key id, in the item",
        "    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:52:27)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
        "    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
        "    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
    ]
}

同样在邮递员中,它返回状态码为 502 Bad gateway,

{
    "message": "Internal server error"
} 

问题:可能是什么问题?即使是很小的帮助也会很有帮助。提前致谢。

【问题讨论】:

    标签: aws-lambda amazon-dynamodb serverless-framework aws-serverless


    【解决方案1】:

    我不确定您是否正确使用了 DocumentClient API。例如,您有

        Item: {
          'id' : {S: `${uuid()}`},
          'title' : {S: `${title}`},
          'status': {S: `OPEN`},
          'createdAt': {S: now.toISOString()}
        }
    

    我只用过这种方式:

        Item: {
          id :  uuid(),
          title : title,
          status: "OPEN",
          createdAt: now.toISOString()
        }
    

    也许这就是 aws-sdk 抱怨缺少 id 密钥的原因。

    【讨论】:

    • 感谢您的回复。我也使用了你建议的方法,但结果还是一样。
    • 该死!我不确定这是不是问题,但希望它对你有用。 console.log(Auction: ${JSON.stringify(params)}); 打印出什么?
    • 它打印这个{"TableName": "AuctionsTable","Item": {"id": "ca1a1e5c-a079-493a-93a4-66ad90f28a4c", "title": "Hi", "status": "OPEN", "createdAt": "2021-02-16T04:52:13.136Z" } }
    • 我将我的表重命名为 AuctionsTable-dev 并且成功了。
    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2015-08-07
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多