1

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.

1 Answer 1

2

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 AttributeValues to simplify the JavaScript development experience with Amazon DynamoDB. JavaScript objects passed in as parameters are marshalled into AttributeValue shapes required by Amazon DynamoDB. Responses from DynamoDB are unmarshalled into plain JavaScript objects by the DocumentClient. The DocumentClient, does not accept AttributeValues 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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.