0

I have userName and className as partition and sort key in dynamo DB table. When inserting duplicate records I am getting error on AWS web console as expected.( Status Code: 400; Error Code: ConditionalCheckFailedException;). Now I am trying to insert the record from lambda function with following code. It is working fine as expected for first time on unique record. But when I am trying to insert the same record is it not supposed to throw me same error ? I am not getting errors as response back. whats wrong with this? FYI.. duplicates records not being inserted though(at least this is happening as expected)..

const AWS = require('aws-sdk');
// const dynamoDB = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
const docClient = new AWS.DynamoDB.DocumentClient({
    region: 'us-east-2',
    apiVersion: '2012-08-10'
});
exports.handler = (event, context, callback) => {
    var params = {
        Item: {
            userName: event.userName,
            className: event.className
        },
        ReturnValues: "ALL_OLD",
        TableName: "IRAT-COLLECTIONS"
    };
    docClient.put(params, function(err, data) {
        if (err) {
            console.log(err)
            callback(err);
        } else {
            console.log('data successfully written')
            console.log(data)
            callback(null, data);
        }
    })
};
1

1 Answer 1

1

Documentation is very clear on PutItem,

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item. You can perform a conditional put operation (add a new item if one with the specified primary key doesn't exist), or replace an existing item if it has certain attribute values. You can return the item's attribute values in the same operation, using the ReturnValues parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.