Home > front end >  Unexpected key '0' found in params.ExpressionAttributeValues
Unexpected key '0' found in params.ExpressionAttributeValues

Time:01-03

I'm trying to replace an array in an dynamodb item, but I'm getting the error

* UnexpectedParameter: Unexpected key '0' found in params.ExpressionAttributeValues[':myArray']
* UnexpectedParameter: Unexpected key '1' found in params.ExpressionAttributeValues[':myArray']
* UnexpectedParameter: Unexpected key '2' found in params.ExpressionAttributeValues[':myArray']
* UnexpectedParameter: Unexpected key '3' found in params.ExpressionAttributeValues[':myArray']
* UnexpectedParameter: Unexpected key '4' found in params.ExpressionAttributeValues[':myArray']

I thought I'm setting the attributes correctly, but I guess not.

dynamo record

{
    "PK": "myItem",
    "myArray": [
     {
      "timeStamp": 1672678545
     },
...more objects
    ]
   }
    let params = {
        UpdateExpression: 'set #myArray = :myArray',
        ExpressionAttributeNames: {
            '#myArray': 'myArray'
        },
        ExpressionAttributeValues: {
            ':myArray': filteredOutObjects
        },

        Key: {
            PK: {
                S: 'myItem'
            }
        },
        TableName: 'configs',
        ReturnValues: 'ALL_NEW'
    };
    try {
        const result = await dynamo.updateItem(params).promise();
        console.log('RESULT', result);
        return result;
    } catch (e) {
        console.log('ERROR', e);
    }

CodePudding user response:

filteredOutObjects should be a suitable data type for DynamoDB and look like the following:

filteredOutObjects = { 'L' : [1,2,3...] }

Or rather:

ExpressionAttributeValues: {
            ':myArray': {
                 'L': filteredOutObjects
                 }
              },

If you want to just pass an array as filteredOutObjects then you should use the Document Client.

The DynamoDB Document client simplifies working with items by abstracting the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, and converts annotated response data to native JavaScript types.

  • Related