0

i am programming in nodejs and i am trying to make an update request in dynamoDB that should execute only if a conditional expression validate to true. For that, I check if in the request body object (parsedBody) there is a key called crypto.

So, this is part of my code (docClient is an instance of AWS.dynamoDB...):

enter image description here

However, if I execute this code, I got this error here:

enter image description here

So, please, can someone explain to me what am I getting wrong here?

Cheers, Marcelo

Turtles
  • 234
  • 3
  • 13

1 Answers1

-1

EDIT:

From AWS Docs,

"You specified a condition that evaluated to false. For example, you might have tried to perform a conditional update on an item, but the actual value of the attribute did not match the expected value in the condition."

So the behavior is correct.

You can try to use https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html to update an Item just in case your condition is satisfied.


The conditional expression is executed on Dynamo side and not on your server.

I think the object persedBody does not exists on DB, so you could try to pass just the 'crypto' as argument of the attribute_exists function.

Something like: ConditionalExpression: 'attribute_exists(crypto)'

Kira
  • 92
  • 3
  • Hi Kira, yes, this I tried but I got the same error... – Turtles Apr 29 '21 at 19:39
  • @Turtles sorry, there is a mistake in what you want to do. If you use ConditionalExpression, you will got an error if the conditional expression return false. This is explained here https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html#Programming.Errors.MessagesAndCodes. You can try https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html to achieve what you want. – Kira Apr 30 '21 at 07:20
  • Hi Kira, thanks for your comment. I believe that it is not possible because the conditional expression check the attributes in the table and not the new attributes passed in the request. However, this is something that I would expect to have. Thanks! – Turtles Apr 30 '21 at 09:29
  • 1
    Maybe I misunderstood your case. Yes, all you define as update option is evaluated on DynamoDB and not on your application. If you need to check that a parameter exists in your request, you need to add some custom validation. – Kira Apr 30 '21 at 10:35