Amazon Web Service:DynamoDB:updateItem提供验证错误

时间:2019-06-16 17:58:18

标签: amazon-web-services amazon-dynamodb

问题:

我正在为AWS的dynamoDB中的updateItem指令苦苦挣扎。据我所知,我已经按照文档中的说明以及Amazon支持技术的指示准确地编写了代码。但是,我收到一条错误消息,其中包含13个验证错误。他们对我没有意义。

我到目前为止所做的事情:

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

我构造了这个Codepen 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"
} 

2 个答案:

答案 0 :(得分:1)

好的,在沉浸于AWS DynamoDB文档大约十年之后,我想出了指定var params0的正确方法。我会将其发布在这里,以防万一有人对未来感兴趣。

any_name = re.compile("\b({})\b".format(pattern))

答案 1 :(得分:0)

延迟回答,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