我在cloudformation中定义了我的dynamoDB如下:
TodosDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: createdAt
AttributeType: S
- AttributeName: todoId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: createdAt
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
TableName: ${self:provider.environment.TODOS_TABLE}
LocalSecondaryIndexes:
- IndexName: ${self:provider.environment.TODOID_INDEX}
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: todoId
KeyType: RANGE
Projection:
ProjectionType: ALL # What attributes will be copied to an index
现在,我想根据userId和todoId查找项目。所以我用了下面的打字本代码:
const key = {
userId: userId,
todoId: todoId
}
await docClient.delete({
TableName: todosTable,
Key: key
}).promise();
但是这给了我一个来自CloudWatch的错误
2020-06-19T18:03:17.349Z 00a89359-428f-4a7d-b63c-500e7d062c1b ERROR Invoke Error
{
"errorType": "ValidationException",
"errorMessage": "The provided key element does not match the schema",
"code": "ValidationException",
"message": "The provided key element does not match the schema",
"time": "2020-06-19T18:03:17.347Z",
"requestId": "7EJTPMFQKEUTQAKNI1UVQ0FRF7VV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 6.9291082443874386,
"stack": [
"ValidationException: The provided key element does not match the schema",
" at constructor.httpResponse (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/protocol/json.js:47:1)",
" at constructor.shift [as callListeners] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:100:1)",
" at constructor.doneCallback [as emit] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:75:1)",
" at constructor.call [as emitEvent] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:683:1)",
" at constructor.emit (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:22:1)",
" at r.call [as runTo] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/state_machine.js:14:1)",
" at runTo (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/state_machine.js:26:1)",
" at constructor.done (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:38:1)",
" at constructor.call (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:685:1)",
" at constructor.Error [as callListeners] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:108:1)"
]
}
基于userId和/或todoId的正确的删除语法是什么?
更新: docClient是从标准样板aws、XRay和DynamoDB创建的
const XAWS = AWSXRay.captureAWS(AWS)
const docClient = new XAWS.DynamoDB.DocumentClient()
我还可以使用
const result = await docClient.query({
TableName: todosTable,
IndexName: todoIdIndex,
KeyConditionExpression: 'userId = :userId and todoId = :todoId',
ExpressionAttributeValues: {
':userId': userId,
':todoId': todoId
}
}).promise()
发布于 2020-06-19 19:47:47
不确定文档客户端是什么,但这应该是您所需要的。基本上先查询,然后删除。这是因为您的表是用userId
和createdAt
索引的,所以您需要在删除条目之前找出它们。
const result = await docClient.query({
TableName: todosTable,
IndexName: todoIdIndex,
KeyConditionExpression: 'userId = :userId and todoId = :todoId',
ExpressionAttributeValues: {
':userId': userId,
':todoId': todoId
},
ProjectionExpression: 'userId, createdAt'
}).promise()
if (result.$response.data && result.$response.data.Items) {
const key = {
userId: result.$response.data.Items[0]['userId'],
createdAt: result.$response.data.Items[0]['createdAt']
}
await docClient.delete({
TableName: todosTable,
Key: key
}).promise()
}
https://stackoverflow.com/questions/62476137
复制相似问题