Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am facing ConditionalCheckFailedException while performing update operation on table in dynamoDB using node js

I am trying to create fake audience app using coveritlive chat system. For this i have created different lambda functions one for posting comments, one for getting item id and one for adding reply to comment. But for implementing reply service i need to get item_id of comment which i can get by calling event/data API of coveritlive.com. But am getting following exception while performing update operation in Amazon DynamoDB. I don't know why this exception is being shown. Also i want to know if there is any bulk update functionality available or not. Please help me in resolving this issue.

ConditionalCheckFailedException: The conditional request failed at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27) at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20) at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:
06179d64-8906-11e8-bc53-f1cb2c5efa4e    { ConditionalCheckFailedException: The conditional request failed
at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
message: 'The conditional request failed',
code: 'ConditionalCheckFailedException',
requestId: 'JVQV9VI69F3T2UDG6388RLDPFBVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 400,
retryable: false,
retryDelay: 4.294909454040329

This is the code i am using for updation:

function updateItemId(id,itemId,docClient) {
    var updateParams = {
        TableName:"comments",
        Key: {
            Id : Number(id)
        },
        UpdateExpression: "set ItemId = :ItemId",
        ConditionExpression: "ItemId = :x",
        ExpressionAttributeValues:{
            ":ItemId":Number(itemId),
            ":x":0
        },
        ReturnValues:"UPDATED_NEW"
    };
    docClient.update(updateParams,function(err,data){
        if(err) {
           console.log(err);
        }
        else{
            console.log(data);
        }
    });
}

I am trying to replace ItemId by calling it frequently.

like image 386
MGupta Avatar asked Apr 25 '26 20:04

MGupta


1 Answers

The reason is that the "ItemId" does not equal 0 in the item which matches the key, so it'll throw an exception named ConditionalCheckFailedException.

If why there is no match doesn't matter, all you need to do is to catch and ignore it.

if (err.code === 'ConditionalCheckFailedException') {
    // nerver mind.
}
like image 54
troy Avatar answered Apr 28 '26 09:04

troy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!