dynamodb add item to the array list

Viewed 378

Using serverless-stack.

I have a table company with multiple branches:

     new sst.Table(this, 'Company', {
        fields: {
            userId: sst.TableFieldType.STRING,
            companyId: sst.TableFieldType.STRING,
            companyName: sst.TableFieldType.STRING,
            branches: [
                {
                    branchId: sst.TableFieldType.STRING,
                    branchName: sst.TableFieldType.STRING
                }
            ]
        },
        primaryIndex: {partitionKey: "userId", sortKey: "companyId"}
    })

I am trying to add branch to the branches:

const branch = {
    branchId: uuid.v1(),
    branchName: data.branchName
}
const params = {
    TableName: process.env.COMPANY_TABLE_NAME,
    Key: {userId: "1", companyId: data.companyId},
    UpdateExpression: "ADD #branches :branch",
    ExpressionAttributeNames: { "#branches" : "branches" },
    ExpressionAttributeValues: { ":branch": [branch] }
 }

But I get this error:

ERROR ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
 ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
2 Answers
Related