dynamodb.put().promise() not returning the put object

I am trying to make use of the async/await functionality with regard to aws and dynamo db. Below is an example of how to put an object pre asyn await, as you can see in the callback you have access to data which contains the put object. However in the second block of code which uses async and promise the result is an empty object, any thoughts?

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html

Non Promise Version

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

var table = "Movies";

var year = 2015;
var title = "The Big New Movie";

var params = {
    TableName:table,
    Item:{
        "year": year,
        "title": title,
        "info":{
            "plot": "Nothing happens at all.",
            "rating": 0
        }
    }
};

console.log("Adding a new item...");
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));
    }
});

Promise async Version – assume the wrapping function is marked async

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

var table = "Movies";

var year = 2015;
var title = "The Big New Movie";

var params = {
    TableName:table,
    Item:{
        "year": year,
        "title": title,
        "info":{
            "plot": "Nothing happens at all.",
            "rating": 0
        }
    }
};
const result: any = await dynamoDb.put(params).promise()
console.log(result) 

According to the doc you have to use ReturnValues if you want something back.

When you’re using promises, you should handle the returned promise object using .then() and .catch(). If you take a look at the documentation, your request should look like this:

dynamoDb.put(params).promise()
  .then(function(data) {
    console.log(data);
  })
  .catch(function(err) {
    console.log(err);
  });

This will also help you see if you are getting any error (same idea as surrounding an await with try/catch, but clearer syntax)

I have tried ReturnValues: 'ALL_OLD'
but in async await it has no result.

Read More:   How to convert local file path to a file::?/ url safely in node.js?

Here is a part of code:

const answersParams = {
          TableName: ANSWERS_TABLE,
          Item: {
            answersId,
            answers,
            userId,
            quizId,
          },
          ReturnValues: 'ALL_OLD',
        };

        try {
          const createdAnswres = await db.put(answersParams).promise();

          return {
            statusCode: 201,
            body: JSON.stringify(createdAnswres && createdAnswres.Item),
          };
        } catch (error) {
          return {
            statusCode: 500,
            body: 'failed to save user answers',
          };
        }
      }
    

So I had to add another request to db:

const createdAnswres = await db.get({
              TableName: ANSWERS_TABLE,
              Key: { answersId },
            })
            .promise();
    


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts