javascript - 使用 JavaScript 进行 AWS DynamoDB 事务 : One or more parameter values were invalid

标签 javascript amazon-web-services transactions amazon-dynamodb

帮助我,欧比旺...

我正在尝试执行 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 examplethe 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 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.

这意味着 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/

相关文章:

json - Dockerrun.aws.json 版本无效,中止部署

javascript - VPC下Lambda访问Api Gateway

sql - GCP Node js API 中的事务提交返回 {"code":10 ,"rowCounts":[]} with spanner

javascript - React分离pdf页面静态

amazon-web-services - 必须允许哪些策略操作才能将文件上传到 s3?

python - Flask错误处理程序,回滚mysql异常

java - 在 Java-EE 中透明地管理每个请求事务

JavaScript 正则表达式无需重复

javascript - 如何从 React.js 中的本地 Json 文件获取图像 URL

javascript - 在javascripts中用汉字将十六进制解码为字符串