1

I have this sample dynamo db create data code

var AWS = require("aws-sdk");
let awsConfig = {
    "region": "ap-south-1",
    "endpoint": "http://dynamodb.ap-south-1.amazonaws.com",
    "accessKeyId": "xxxxxxxxxxxxxxxxx", "secretAccessKey": "xxxxxxxxxxxxxxxxxxxxxxx"
};
AWS.config.update(awsConfig);

let docClient = new AWS.DynamoDB.DocumentClient();

console.log(docClient);

let save = function () {

    var input = {
        "task": "[email protected]"
    };
    var params = {
        TableName: "todos",
        Item:  input
    };
    docClient.put(params, function (err, data) {

        if (err) {
            console.log("error - " + JSON.stringify(err, null, 2));                      
        } else {
            console.log(data);
        }
    });
}



save();

Now I converted this into async-await form

const create = async (docClient,payload,tableName) =>{
    try {
        console.log(payload);
        const createdDoc = await docClient.put({TableName:tableName,Item:payload})
        console.log(createdDoc);
    } catch (error) {
       console.log(error); 
    }
}
        


create(docClient,{"task":"sample"},"todos")

This code neither gave me an error neither it add data into dynamo DB tables

1
  • Does put return a promise? If not, then wrap the entire call in a promise and call resolve() in your callback when you're done. Commented Dec 2, 2021 at 13:04

1 Answer 1

1

You have to call .promise() on the result of .put() call:

const createdDoc = await docClient.put({ … }).promise();

See docs for AWS.DynamoDB.DocumentClient.put() and AWS.Request.promise().

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.