当我尝试点击 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)