Search code examples
amazon-dynamodbaws-sdkaws-sdk-nodejs

(DynamoDB) ConditionExpression behaves unpredictably with an '=' operator. How could I debug it better?


My objects in Dynamodb look roughly like this:

{ 
  userId: "GEFOeE8EsaWmq4NQ3oh7tbeVkLx1",
  url: 'objectURL', 
  object: {}
}

I have this simple piece of code for deleting an object, when the user that owns the object requests a delete. The user argument here is a parsed JWT, by the way.

export async function deleteObject(user, url) {
    let params = {
        TableName: OBJECTS_TABLE,
        Key: {
            url: url,
        },
        ConditionExpression: `userId = :uid`,
        ExpressionAttributeValues: {
            ":uid": {
                S: user.sub
            }
        }
    };
    let deleteResult = await dynamoDb.delete(params).promise();
    return deleteResult;
}

The problem is that it doesn't work, and I've made sure that the problem stems from the ConditionExpression by changing = to <>. I simply get this: ConditionalCheckFailedException: The conditional request failed

I'm sure solving the problem wouldn't be difficult, but I barely have any information

Questions:

  1. Why is the condition expression failing? Everything looks alright, and it should work. Right?
  2. How could I debug this issue better?

Solution

  • I figured it out. ExpressionAttributeValues can be directed used, without mentioning the datatype. The Javascript SDK does that automatically.

    export async function deleteObject(user, url) {
        let params = {
            TableName: OBJECTS_TABLE,
            Key: {
                url: url,
            },
            ConditionExpression: `userId = :uid`,
            ExpressionAttributeValues: {
                ":uid": user.sub
            }
        };
        let deleteResult = await dynamoDb.delete(params).promise();
        return deleteResult;
    }