简体   繁体   English

带有JavaScript的AWS DynamoDB事务:一个或多个参数值无效

[英]AWS DynamoDB Transactions with JavaScript: One or more parameter values were invalid

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): 我正在尝试做一个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"
      }
    }
  ]
}

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) . 我应该指出,主分区键是slug (String)和主排序键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? 因此,我不了解Type mismatch for key slug expected: S actual: MType mismatch for key slug expected: S actual: M短语:如果期望S ,那是我发送的,对吗? I don't see an M in there anywhere. 我在任何地方都看不到M

Note the following when working with the Document Client (which offers a higher level API than the DynamoDB class ): 使用文档客户端 (提供比DynamoDB更高级别的API)时,请注意以下几点:

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. 文档客户端通过抽象化属性值的概念,简化了在Amazon DynamoDB中处理项目的工作。 This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types . 此抽象对作为输入参数提供的本机JavaScript类型进行注释,并将带注释的响应数据转换为本机JavaScript类型

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类型而非AttributeValue的功能,以简化Amazon DynamoDB的JavaScript开发体验。 JavaScript objects passed in as parameters are marshalled into AttributeValue shapes required by Amazon DynamoDB. 作为参数传入的JavaScript对象被编组为Amazon DynamoDB所需的AttributeValue形状。 Responses from DynamoDB are unmarshalled into plain JavaScript objects by the DocumentClient . DynamoDB的响应被DocumentClient解组到普通的JavaScript对象中。 The DocumentClient , does not accept AttributeValue s in favor of native JavaScript types . DocumentClient 不接受AttributeValue来支持本机JavaScript类型

This means that slug must be a plain string ( S ) and not a map ( M ) with attribute type. 这意味着, slug必须是纯字符串( S ),而不是具有属性类型的映射( M )。

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. 直接使用DynamoDB (较低级别)时,必须指定属性类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM