Search code examples
node.jsamazon-dynamodb

Dynamodb update list_append show ValidationException using nodejs


The database table structure is

{
 "phone": "01131845602", //primary key
 "customerId": "47E1395C-08DD-E6B1-EEF9", //sort key
 "consultations": [
  {
   "consultId": "BE9320FB-7635-499E-83A2",
   "header": "asdfadfadsfasdfa",
   "body": [
    "asdfasdfasdf",
    "asdfasdfasdf"
   ],
   "footer": {
    "consentButtonText": "afadsfasdfas",
    "consentText": "asdfasdfasdfasdfasdfasdfasdf",
    "isCheckbox": true
   }
  }
 ]
}

I want to push a consultation object to the table 'consultations' field. 'phone' is primary key. My new consultation object is same.

{
   "consultId": "BE9320FB-7635-499E-83A3",
   "header": "asdfadfadsfasdfa",
   "body": [
    "asdfasdfasdf",
    "asdfasdfasdf"
   ],
   "footer": {
    "consentButtonText": "afadsfasdfas",
    "consentText": "asdfasdfasdfasdfasdfasdfasdf",
    "isCheckbox": true
   }
  }

When I tried to update. Its giving me the exception

  ValidationException: The provided key element does not match the schema
  ...
  ...
  code: 'ValidationException',
  time: ...
  requestId: '....',
  statusCode: 400,
  retryable: false,
  retryDelay: 30.830076736357114

My update function code

try {
        params = {
          TableName: tablename,
          Key: { phone: phone },
          UpdateExpression: "SET #co = list_append(#co, :data)",
          ExpressionAttributeNames: {
            '#co': 'consultations'
          },
          ExpressionAttributeValues: {
            ':data': [data]
          },
          ReturnValues: "ALL_NEW"
        };
        
        const result = await this.awsDynamoDb.update(params);
        console.log("result => ", result);
      } catch (error) {
        console.log("error updating -> ", error);
      }

I'm expecting to update the field in this format

{
 "phone": "01131845602",
 "customerId": "47E1395C-08DD-E6B1-EEF9",
 "consultations": [
  {
   "consultId": "BE9320FB-7635-499E-83A2",
   "header": "asdfadfadsfasdfa",
   "body": [
    "asdfasdfasdf",
    "asdfasdfasdf"
   ],
   "footer": {
    "consentButtonText": "afadsfasdfas",
    "consentText": "asdfasdfasdfasdfasdfasdfasdf",
    "isCheckbox": true
   }
  },
  {
   "consultId": "BE9320FB-7635-499E-83A2",
   "header": "asdfadfadsfasdfa",
   "body": [
    "asdfasdfasdf",
    "asdfasdfasdf"
   ],
   "footer": {
    "consentButtonText": "afadsfasdfas",
    "consentText": "asdfasdfasdfasdfasdfasdfasdf",
    "isCheckbox": true
   }
  }
 ]
}

I don't understand why both same format data would give schema error. Please help me where am I doing wrong?


Solution

  • You don't show which client you are using. I'll make a guess here and bet you are using the low level client, and not the DocumentClient, which is why it seems to give you this error. Some other things to check:

    • check describe table output and be sure you are including both the partition and sort key in your request
    • be sure the data type for both the partition and sort key are correct
    • double check the table name you are using is correct
    • double check the region is correct.