1

The Problem:

I'm struggling with the updateItem instruction in AWS' dynamoDB. So far as I can tell, I've written the code precisely as illustrated in the documentation and as directed by an Amazon support tech. Nonetheless, I receive an error message with 13 validation errors. They don't make sense to me.

What I've Done So Far:

I've searched StackOverflow and read the few AWS DynamoDB questions. None was on point. I searched the web generally and found no help. I've been reading the AWS DynamoDB reference material, but it's dense, and the going is slow. I'm still working on that.

I constructed this codepen minCase pin.

Any help will be appreciated in the extreme.

The JavaScript Code

    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

The Error Message: 13 Validation Errors.

Following is the console log. I logged the params object so that you could see what was being submitted to 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 Answers 2

2

OK, after about a decade immersed in the AWS DynamoDB documentation, I figured out the correct way to specify the var params0. I'll post it here just in case anyone is interested in the future.

params0 = {
           'TableName': 'FHCRoomCounts',
           'Key': {
                   'timeSlot': {
                                'S': '0830'
                               },
                    'room': {
                              'S': 'chapel'

                            }
                 },
          'ExpressionAttributeValues': {
                                          ':n': {'N': '1'},
                                       },
          'UpdateExpression':'ADD registrants :n',
          'ReturnValues': 'ALL_NEW', 

          };
Sign up to request clarification or add additional context in comments.

2 Comments

My thanks and gratitude to everyone who took the time to read the question, even the two downvoters. It would help me to better formulate future questions if they would explain the reasons for the downvotes. I want to read all input, even the negative. That's the way one learns.
I would like to contribute my 1 upvote to your question. :) @alxfyv
1

Late to answer, DynamoDB is tricky in that, once the datatype is set, you have to make sure it is always interpreted in the same way in your inserts and updates etc. Sometimes this is done nicely by a DynamoDBMarshaller, other times you just have to give it the right types yourself like when using javascript.

Also useful to note, ExpressionAttributeValues is only required if you are using a keyname that might conflict with dynamodb reserved words. 'COUNTER' is a reserved word.

    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
    },

The types are N,S,BOOL,B,SS , date for example is an 'S' type.

Helpful links:

Data Types

Reserved Words

1 Comment

Thanks a lot, @jan for such a valuable piece of information. Your explanation really helped me a lot, appreciate it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.