1

I'm trying with the following code

{
        ExpressionAttributeNames: {
            "#items": "items"
        },
        ExpressionAttributeValues: {
            ":item": [slug]
        },
        Key: {
            listId: listId,
            userId: userData.userId,
        },
        UpdateExpression: "SET #items = list_append(#items,:item)",
        ConditionExpression: "NOT contains (#items, :item)",
        TableName: process.env.listsTableName,
    }

but the item is still added even if string already exists in the list. What am I doing wrong?

The list structure is like so:

        {
            Item: {
                userId: userData.userId,
                listId: crypto.createHash('md5').update(Date.now() + userData.userId).digest('hex'),
                listName: 'Wishlist',
                items: [],
            },
            TableName: process.env.listsTableName,
        };

Later Edit: I know I should use SS as it does the condition for me but SS doesn't work in my context because SS can't be empty.

1 Answer 1

1

As the documentation explains, the contains() function only works on a string value (checking for a substring) or a set value (checking for membership). But in your case, you don't have a set but rather a list - with are different things in DynamoDB.

If all the items which you want to add to this list are strings, and you anyway don't want duplicates in the list, the most efficient way would be to stop using a list, and instead use the set-of-strings (a.k.a. SS) type. To add an item to the set (without duplicates), you would simply use "ADD #items :item" (no need for any additional condition - duplicates will not be added).

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

1 Comment

SS doesn't work for me because it doesn't allow empty sets.

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.