Harishfysx
Harishfysx

Reputation: 81

Dynamo DB- Lambda Function Not returning error?

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);
        }
    })
};

Upvotes: 0

Views: 1583

Answers (1)

Kannaiyan
Kannaiyan

Reputation: 13025

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.

Upvotes: 1

Related Questions