0
votes

So I have an item in DynamoDB:

{
  "id": "1",
  "foo": "bar"
}

And I want to add a new attribute to said item, the attribute being:

{
  "newAttr": [{ "bar": 1 }, { "bar": 2 }, { "bar": 3 }]
}

Now I'm doing this in JS using the AWS-SDK like so:

client.update({
  ExpressionAttributeNames: { '#newAttr': 'newAttr' },
  ExpressionAttributeValues: { ':newAttr': newAttr },
  Key: { "id": "1" },
  TableName: "foo",
  UpdateExpression: "SET #newAttr = :newAttr"
}, callback)

However, I get an error:

ExpressionAttributeValues contains invalid value: One or more parameter values were invalid: An AttributeValue may not contain an empty string for key :newAttr

So if I were to JSON.stringify(newAttr) then it's fine, but I don't want this new attribute to be a string, I want it to be a list.

So what am I doing wrong?

1

1 Answers

0
votes

Try ADD instead of SET

    client.update({
  ExpressionAttributeNames: { '#newAttr': 'newAttr' },
  ExpressionAttributeValues: { ':newAttr': newAttr },
  Key: { "id": "1" },
  TableName: "foo",
  UpdateExpression: "ADD #newAttr = :newAttr"
}, callback)

I ended up having these issues in the beginning. I'm sure you are wasting time in writing in all the low-level code to interact with DynamoDB.

Use an ODM (ORM Like) for document storage which handles all the hurdles for you.

https://github.com/clarkie/dynogels

is what I have used and will make your life much easier.

Empty String are not allowed

One cannot store empty strings with DynamoDB at the root level. You can have it in the nested objects.

Allowed Data Types from DynamoDB:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html

Hope this helps.