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);
}
})
};
PutItemoperation will overwrite an item with the same key (if it exists). If you want to avoid this, use a condition expression."