Reputation: 521
Help me, Obi-Wan...
I'm trying to do a docClient.transactWrite(params)
, where my params
look like (there are other fields too, trying to keep this fairly short):
{
"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"
}
}
]
}
As near as I can tell by looking at this example and the official documentation it's fine, but whenever I run it I get to following error:
ERROR Error performing transactWrite { cancellationReasons:
[ { Code: 'ValidationError',
Message:
'One or more parameter values were invalid: Type mismatch for key slug
expected: S actual: M' } ],
I should point out that the Primary partition key is slug (String)
and the Primary sort key tag (String)
. So I don't understand the Type mismatch for key slug expected: S actual: M
phrase: If it's expecting S
, well, that's what I sent, right? I don't see an M
in there anywhere.
Upvotes: 1
Views: 546
Reputation: 7407
Note the following when working with the Document Client (which offers a higher level API than the DynamoDB
class):
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.
This means that slug
must be a plain string (S
) and not a map (M
) with attribute type.
The following should work:
{
"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"
}
}
]
}
When working directly with the DynamoDB
class (lower level) attribute types must be specified.
Upvotes: 2