【问题标题】:Amazon Web Service: DynamoDB: updateItem giving validation errorsAmazon Web Service:DynamoDB:updateItem 给出验证错误
【发布时间】:2019-06-16 17:58:18
【问题描述】:

问题:

我正在努力处理 AWS dynamoDB 中的 updateItem 指令。据我所知,我已经完全按照文档中的说明以及亚马逊支持技术人员的指示编写了代码。尽管如此,我还是收到了包含 13 个验证错误的错误消息。它们对我没有意义。

到目前为止我做了什么:

我搜索了 StackOverflow 并阅读了几个 AWS DynamoDB 问题。没有一个是正确的。我一般在网上搜索并没有找到任何帮助。我一直在阅读 AWS DynamoDB 参考资料,但它很密集,而且进展缓慢。我还在努力。

我构建了这个代码笔minCase pin

任何帮助将不胜感激。

JavaScript 代码

    var dynamoDB = new AWS.DynamoDB();
    params0 = {
               'TableName': 'FHCRoomCounts',
               'Key': {
                          timeSlot: '0830',
                        room: 'chapel',
                      },
                'UpdateExpression': 'ADD registrants :n',
               'ExpressionAttributeValues': {
                                              ':n' : 1,
                                            },
               'ReturnValues': 'ALL_NEW' 
           };

          console.log('log params0 : ');
          for(let [key, value] of Object.entries(params0)) {
            console.log(key, value);
          } // end for-let
          console.log('calling dynamoDB.updateItem');

    dynamoDB.updateItem(params0, function(err, data) {
      if (err) {
              console.log('Unable to update registrant: ' + '\n' + JSON.stringify(err, undefined, 2));
  } else {
              console.log('Increase registrant succeeded: ' + '\n' + JSON.stringify(data, undefined, 2));
          }  // end else

错误消息:13 个验证错误。

以下是控制台日志。我记录了 params 对象,以便您可以看到提交给 AWS 的内容。

    jquery loaded 
    AWS sdk loaded
    log params0 : 
    TableName FHCRoomCounts
    Key Object { timeSlot: "0830", room: "chapel" }
    UpdateExpression ADD registrants :n 
    ExpressionAttributeValues Object { ":n": 1 }
    ReturnValues ALL_NEW 
    calling dynamoDB.updateItem 
    Unable to update registrant: 
    {
       "message": "There were 13 validation errors:\n*     

    InvalidParameterType: Expected params.Key['timeSlot'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Key['timeSlot']\n* UnexpectedParameter: Unexpected key '1' found in params.Key['timeSlot']\n* UnexpectedParameter: Unexpected key '2' found in params.Key['timeSlot']\n* UnexpectedParameter: Unexpected key '3' found in params.Key['timeSlot']\n* InvalidParameterType: Expected params.Key['room'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Key['room']\n* UnexpectedParameter: Unexpected key '1' found in params.Key['room']\n* UnexpectedParameter: Unexpected key '2' found in params.Key['room']\n* UnexpectedParameter: Unexpected key '3' found in params.Key['room']\n* UnexpectedParameter: Unexpected key '4' found in params.Key['room']\n* UnexpectedParameter: Unexpected key '5' found in params.Key['room']\n* InvalidParameterType: Expected params.ExpressionAttributeValues[':n'] to be a structure",
  "code": "MultipleValidationErrors",
  "errors": [
    {
      "message": "Expected params.Key['timeSlot'] to be a structure",
      "code": "InvalidParameterType",
      "time": "2019-06-15T04:51:05.288Z"
    },
    {
      "message": "Unexpected key '0' found in params.Key['timeSlot']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.289Z"
    },
    {
      "message": "Unexpected key '1' found in params.Key['timeSlot']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.289Z"
    },
    {
      "message": "Unexpected key '2' found in params.Key['timeSlot']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.289Z"
    },
    {
      "message": "Unexpected key '3' found in params.Key['timeSlot']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.289Z"
    },
    {
      "message": "Expected params.Key['room'] to be a structure",
      "code": "InvalidParameterType",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '0' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '1' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '2' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '3' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '4' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Unexpected key '5' found in params.Key['room']",
      "code": "UnexpectedParameter",
      "time": "2019-06-15T04:51:05.290Z"
    },
    {
      "message": "Expected params.ExpressionAttributeValues[':n'] to be a structure",
      "code": "InvalidParameterType",
      "time": "2019-06-15T04:51:05.291Z"
    }
  ],
  "time": "2019-06-15T04:51:05.291Z",
  "stack": "validate@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:61:19246\n[311]</a.EventListeners.Core</<@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:60:25947\ncallListeners@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:26769\ns@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:26635\n[311]</a.EventListeners.Core</</<@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:60:25280\nt@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:59:25669\ngetCredentials/</<@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:59:25990\nget@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:59:30534\ngetCredentials/<@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:59:25896\ngetCredentials@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:59:25997\n[311]</a.EventListeners.Core</<@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:60:25149\ncallListeners@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:26740\nemit@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:26480\nemitEvent@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:12277\ne@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:7919\n[363]</i.prototype.runTo@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:64:9533\nrunTo@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:9575\nsend@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:9468\nmakeRequest@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:62:30482\ndefineMethods/</e.prototype[t]@https://sdk.amazonaws.com/js/aws-sdk-2.470.0.min.js:63:5136\nupdateTable@https://steve-alexander-pfmi.squarespace.com/test-register-2019:110:12\n"
} 

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb


    【解决方案1】:

    迟到了,DynamoDB 的棘手之处在于,一旦设置了数据类型,您必须确保在插入和更新等过程中始终以相同的方式对其进行解释。有时这由 DynamoDBMarshaller 很好地完成,其他时候你只需要自己给它正确的类型,就像使用 javascript 时一样。

    还需要注意的是,仅当您使用可能与 dynamodb 保留字冲突的键名时才需要 ExpressionAttributeValues。 'COUNTER' 是保留字。

        TableName: 'MyTable',
        Key: {
            id: { S: '100001' }, // its forced as a string type by the 'S'
        },
        UpdateExpression: 'set counter=:c', // can be directly set if its not a reserved word
        ConditionExpression: 'attribute_exists(id)', // conditional only update if exists
        ExpressionAttributeValues: {
            // all below are reserved word safe
            ':c': { N: '500' }, // its forced as a number
            //':c': '500', // its not forced to a type, so it will try a string type
        },
    

    类型为 N,S,BOOL,B,SS ,例如日期为 'S' 类型。

    有用的链接:

    Data Types

    Reserved Words

    【讨论】:

    • 非常感谢@jan 提供了如此宝贵的信息。你的解释对我帮助很大,不胜感激! :)
    【解决方案2】:

    好的,在沉浸在 AWS DynamoDB 文档中大约十年之后,我找到了指定 var params0 的正确方法。我会把它贴在这里以防万一有人对未来感兴趣。

    params0 = {
               'TableName': 'FHCRoomCounts',
               'Key': {
                       'timeSlot': {
                                    'S': '0830'
                                   },
                        'room': {
                                  'S': 'chapel'
    
                                }
                     },
              'ExpressionAttributeValues': {
                                              ':n': {'N': '1'},
                                           },
              'UpdateExpression':'ADD registrants :n',
              'ReturnValues': 'ALL_NEW', 
    
              };
    

    【讨论】:

    • 我感谢并感谢花时间阅读问题的每个人,甚至是两个投反对票的人。如果他们能解释反对票的原因,这将有助于我更好地制定未来的问题。我想阅读所有输入,即使是负面的。这就是学习的方式。
    • 我想为您的问题投 1 票。 :) @alxfyv
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2015-03-21
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多