0

I am able to fetch the record from dynamo db and view the response successfully. I need to modify the fetched 'ACCOUNTNAME' attribute in the 'items' array and update the json and also update in dynamo db. Now when I try to update the fetched records I end up with the Invalid attribute value type exception.

I was trying to update it using the key with Array of Strings which is provided with code snippet also tried to update inside for loop using the individual string but both failed with same exception as

  "statusCode": 400,
  "body": {
    "message": "Invalid attribute value type",
    "error": {
      "errorMessage": "ValidationException"
    }
  }

I tried to create params and update the call inside the for loop by setting the key as below,

      Key: {
       "UUID": {
            "S": usersOfAccountFromDB.body.Items[key].UUID
        }
        ,
       "TYPE": {
            "S": user
        }  
      }

but also failed with the same exception.

Fetched Json from dynamo db

[
  {
    "DEFINITION": "914ba44a-8c26-4b60-af0f-96b6aa37efe6",
    "UUID": "830a49cb-4ed3-41ae-b111-56714a71ab98",
    "TYPE": "USER",
    "RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
    "ACCOUNTNAME": "Wolff LLC"
  },
  {
    "DEFINITION": "1f60fded-323d-40e1-a7f8-e2d053b0bed0",
    "UUID": "47db3bbe-53ac-4e58-a378-f42331141997",
    "TYPE": "USER",
    "RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
    "ACCOUNTNAME": "Wolff LLC"
  },
  {
    "DEFINITION": "05ddccba-2b6d-46bd-9db4-7b897ebe16ca",
    "UUID": "e7290457-db77-48fc-bd1a-7056bfce8fab",
    "TYPE": "USER",
    "RELATION": "01efd131-6a5d-4068-889e-9dba44262da5",
    "ACCOUNTNAME": "Wolff LLC"
  },
.
.
.
.]

Now I tried to iterate the Json and setup UUID which is the key as the String array as below,

    var userUUIDArray : string[] = [];
    for (let key in usersOfAccountFromDB.body.Items) {
      userUUIDArray.push(usersOfAccountFromDB.body.Items[key].UUID);
    }

    for (var uuid of userUUIDArray) {
    console.log("UUID : " +uuid);     // prints all the uuid
    }

    // Creating a parameter for the update dynamo db
    var params = {
    TableName: <tableName>,
    Key: {
      "UUID": {
            "SS": userUUIDArray
        }
        ,
      "TYPE": {
            "S": user
        }  
    },               
    UpdateExpression: 'SET #ACCOUNTNAME = :val1',
    ExpressionAttributeNames: {
        '#ACCOUNTNAME': 'ACCOUNTNAME'       //COLUMN NAME   
    },
    ExpressionAttributeValues: {
      ':val1': newAccountName  
    },
    ReturnValues: 'UPDATED_NEW',
    };

    //call the update of dynamodb
    const result = await this.getDocClient().update(param).promise();

I get the error as below,

    "body": {
    "message": "Invalid attribute value type",
    "error": {
      "errorMessage": "ValidationException"
      }
    }

All the approaches failed with same above exception

2 Answers2

0

The update operation which your code currently uses only allow a single item to be updated.

IIUC, you want to update multiple items with one API call. For this you need to use batchWrite operation. Keep in mind that you cannot update more than 25 items per invocation.

The origin of the error you are getting

Your code fails due to the use of "SS" in the UUID field. This field is of type string so you must use "S". Note however that since you're using the document client API you do not need to pass values using this notation. See this answer for further details.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
0

I have resolved the issue now by running the update statement one by one using loop

for (let key in usersOfAccountFromDB.body.Items) {

  var updateParam = {
    TableName: process.env.AWS_DYNAMO_TABLE,
    Key: {
      UUID: usersOfAccountFromDB.body.Items[key].UUID,      
      TYPE: user 
    },     
    UpdateExpression: "SET #ACCOUNTNAME = :val1",
    ExpressionAttributeNames: {
        '#ACCOUNTNAME': 'ACCOUNTNAME'   
    },
    ExpressionAttributeValues: {
        ":val1": newAccountName
    },
    ReturnValues: "UPDATED_NEW",
  };

  const result = await this.getDocClient().update(updateParam).promise();                   
}
Itay Maman
  • 30,277
  • 10
  • 88
  • 118