4

I am using DynamoDB local and can create and delete table. I created a table with only one key like below

const tablePromise = dynamodb.listTables({})
    .promise()
    .then((data) => {
        const exists = data.TableNames
            .filter(name => {
                return name === tableNameLogin;
            })
            .length > 0;
        if (exists) {
            return Promise.resolve();
        }
        else {
            const params = {
                TableName: tableNameLogin,
                KeySchema: [
                    { AttributeName: "email", KeyType: "HASH"},  //Partition key

                ],
                AttributeDefinitions: [
                    { AttributeName: "email", AttributeType: "S" },
                ],
                ProvisionedThroughput: {
                    ReadCapacityUnits: 10,
                    WriteCapacityUnits: 10
                }
            };
            dynamodb.createTable(params, function(err, data){
              if (err) {
                console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
              } else {
                console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
              }
            });
        }
    });

Now I want to insert an item in the table following example doc at AWS.

var docClient = new AWS.DynamoDB.DocumentClient();
var tableNameLogin = "Login"
var emailLogin = "abc@gmail.com";

var params = {
    TableName:tableNameLogin,
    Item:{
        "email": emailLogin,
        "info":{
            "password": "08083928"
        }
    }
};

docClient.put(params, function(err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));

    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});

When I run the insert item code, I get Added item: {} Why does it output an empty object? Is it actually inserting anything? I looked into this callback example but this time it doesn't output anything.

nad
  • 2,640
  • 11
  • 55
  • 96

1 Answers1

3

You need to add ReturnValues: 'ALL_OLD' to your put params. It will look like as mentioned below.

var params = {
    TableName:tableNameLogin,
    Item:{
        "email": emailLogin,
        "info":{
            "password": "08083928"
        }
    },
    ReturnValues: 'ALL_OLD'
};

For more details, you can follow this https://github.com/aws/aws-sdk-js/issues/803

Ajay
  • 81
  • 4