帮助我,欧比旺...
我正在尝试执行 docClient.transactWrite(params)
,其中我的 params
看起来像(还有其他字段,试图保持相当短) :
{
"TransactItems": [
{
"Put": {
"Item": {
"slug": {
"S": "marbled_crockpot_cheesecake"
},
"tag": {
"S": "metadata"
},
"recipe_name": {
"S": "Marbled Crockpot Cheesecake"
}
},
"TableName": "recipes-table-dev"
}
},
{
"Put": {
"Item": {
"slug": {
"S": "marbled_crockpot_cheesecake"
},
"tag": {
"S": "marbled"
},
"recipe_name": {
"S": "Marbled Crockpot Cheesecake"
}
},
"TableName": "recipes-table-dev"
}
}
]
}
据我所知,通过查看this example和 the official documentation很好,但是每当我运行它时,我都会遇到以下错误:
ERROR Error performing transactWrite { cancellationReasons:
[ { Code: 'ValidationError',
Message:
'One or more parameter values were invalid: Type mismatch for key slug
expected: S actual: M' } ],
我应该指出,主分区键是slug(字符串)
,主排序键是tag(字符串)
。所以我不明白 Type Mismatch for key slug预期:S实际:M
短语:如果它期待S
,那么,这就是我发送的,对吧?我在任何地方都没有看到 M
。
最佳答案
使用 Document Client 时请注意以下事项(它提供比 DynamoDB
class 更高级别的 API):
The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.
The document client affords developers the use of native JavaScript types instead of
AttributeValue
s to simplify the JavaScript development experience with Amazon DynamoDB. JavaScript objects passed in as parameters are marshalled intoAttributeValue
shapes required by Amazon DynamoDB. Responses from DynamoDB are unmarshalled into plain JavaScript objects by theDocumentClient
. TheDocumentClient
, does not acceptAttributeValue
s in favor of native JavaScript types.
这意味着 slug
必须是纯字符串 (S
),而不是具有属性类型的映射 (M
)。
以下应该有效:
{
"TransactItems": [
{
"Put": {
"Item": {
"slug": "marbled_crockpot_cheesecake",
"tag": "metadata",
"recipe_name": "Marbled Crockpot Cheesecake",
},
"TableName": "recipes-table-dev"
}
},
{
"Put": {
"Item": {
"slug": "marbled_crockpot_cheesecake",
"tag": "marbled",
"recipe_name": "Marbled Crockpot Cheesecake"
},
"TableName": "recipes-table-dev"
}
}
]
}
直接使用 DynamoDB
class 时必须指定(较低级别)属性类型。
关于javascript - 使用 JavaScript 进行 AWS DynamoDB 事务 : One or more parameter values were invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57530155/