失败:ValidationException:检测到 1 个验证错误:“tableName”处的值“[object Object]”未能满足约束

问题描述

当我尝试点击 createAuction URL 时,我收到此错误,同时离线运行无服务器

请告诉我该怎么做才能消除此错误

无服务器 YAML 文件

service: AuctionService

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

provider:
  name: aws
  runtime: nodejs12.x
  memorySize: 256
  stage: ${opt:stage,'dev'}
  region: us-east-1
  timeout: 5
  endpointType: regional
  environment:
    AUCTIONS_TABLE_NAME: ${self:custom.AuctionsTable.name}
  lambdaHashingVersion: 20201221
  iamRoleStatements:
    - ${file(iam/AuctionsTableIAM.yml):AuctionsTableIAM}

resources:
  Resources:
    AuctionsTable: ${file(resources/AuctionsTable.yml):AuctionsTable}
    
functions:
  createAuction:
    handler: src/handlers/createAuction.handler
    events:
      - http:
          method: POST
          path: /auction
          cors:
            origin: '*'
            headers: ${self:custom.allowedHeaders}
            

custom:
  AuctionsTable:
    name: !Ref AuctionsTable
    arn:  !GetAtt AuctionsTable.Arn
  bundle:
    linting: false
  allowedHeaders:
    - Accept
    - Content-Type
    - Content-Length
    - Authorization
    - X-Amz-Date
    - X-Api-Key
    - X-Amz-Security-Token
    - X-Amz-User-Agent

资源表:YAML 文件

AuctionsTable:
  Type: AWS::DynamoDB::Table
  DeletionPolicy: Retain
  Properties:
    TableName: AuctionsTable-${self:provider.stage}
    AttributeDeFinitions:
      - AttributeName: id
        AttributeType: S
      - AttributeName: status
        AttributeType: S
      - AttributeName: endingAt
        AttributeType: S
    KeySchema:
      - AttributeName: id
        KeyType: HASH
    GlobalSecondaryIndexes:
      - IndexName: statusAndEndDate 
        KeySchema:
          - AttributeName: status
            KeyType: HASH
          - AttributeName: endingAt
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1

createAuction.js

import { v4 as uuid } from "uuid";
import AWS from "aws-sdk";
import commonMiddleware from "../lib/commonMiddleware";
import createError from "http-errors";
import validator from "@middy/validator";
import createAuctionSchema from "../lib/schemas/createAuctionSchema";
import { getResponseHeaders } from "../lib/util/utilityFunctions";

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

async function createAuction(event,context) {
  const { title } = event.body;
  const Now = new Date();
  const endDate = new Date();
  endDate.setHours(Now.getHours() + 1);

  const auction = {
    id: uuid(),title: title,status: "OPEN",createdAt: Now.toISOString(),endingAt: endDate.toISOString(),highestBid: {
      amount: 0,},};

  try {
    await dynamodb
      .put({
        TableName: process.env.AUCTIONS_TABLE_NAME,Item: auction,})
      .promise();
  } catch (e) {
    console.log({ e });
    throw new createError.InternalServerError(e);
  }

  return {
    headers: getResponseHeaders(),statusCode: 201,body: JSON.stringify(auction),};
}

export const handler = commonMiddleware(createAuction).use(
  validator({ inputSchema: createAuctionSchema })
);

错误堆栈:

Serverless offline: Failure: ValidationException: 1 validation error detected: Value '[object Object]' at 'tableName' Failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+
    InternalServerError: ValidationException: 1 validation error detected: Value '[object Object]' at 'tableName' Failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+ at createAuction (D:\code\auction_service\.webpack\service\src\handlers\webpack:\D:\code\auction_service\src\handlers\createAuction.js:37:11)  

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)