我似乎在使用 AWS.DynamoDB.DocumentClient().get() 查询数据时遇到了很多困难。
我正在使用无服务器并serverless.yml
使用此架构设置我的:
resources:
Resources:
ShortUrlsTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: longUrl
AttributeType: S
- AttributeName: shortPath
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: longUrlIndex
KeySchema:
- AttributeName: longUrl
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
Projection:
ProjectionType: ALL
- IndexName: shortPathIndex
KeySchema:
- AttributeName: shortPath
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
我想要做的是在数据库中搜索shortUrlItem
使用longUrl
or 或shortPath
.
到目前为止,我已经设置好了:
dynamoDb = new AWS.DynamoDB.DocumentClient()
app.get("/:longUrl", (req, res) => {
const {longUrl} = req.params
const getParams = {
TableName: SHORT_URLS_TABLE,
Key: {longUrl},
}
dynamoDb.get(getParams, (error, result) => {
res.send({...error, ...result})
})
})
我似乎得到的只是这条错误消息返回给我:
"message":"The provided key element does not match the schema","code":"ValidationException","time":"2018-08-17T20:39:27.765Z","requestId":"4RKNVG7ET1ORVF10H71M7AUABRVV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":21.513795782119505,"TableName":"short-urls-table-dev"
我似乎无法弄清楚我是否正确查询或正确设置我的架构,以便二级索引成为我表中的可搜索键。