Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)  
like image 818
Mingo Avatar asked Mar 14 '19 15:03

Mingo


People also ask

What is put in DynamoDB?

Creates a new item, or replaces an old item with a new item.

Which is the method of DynamoDB document client is used for returning one or more items and item attributes by accessing every item in a table?

putItem() . Directly access items from a table by primary key or a secondary index. Returns one or more items and item attributes by accessing every item in a table or a secondary index.

What is DocumentClient?

DocumentClient object. Create a JSON object containing the parameters needed to write an item to the table, which in this example includes the name of the table and a description of the item to add or update that includes the hashkey and value as well as names and values for attributes to set on the item.

What is Keyconditionexpression in DynamoDB?

This allows Query to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values. from the docs. It is not supported by DynamoDB and it is actually to save your money.


2 Answers

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

like image 144
Gabriel Bleu Avatar answered Oct 04 '22 03:10

Gabriel Bleu


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)

like image 36
Deiv Avatar answered Oct 04 '22 02:10

Deiv