0
votes

Can anyone help me here, I have tried everything: The get and getItem functions DO NOT return anything ( in the loop, the promise function) although there is a record in the dynamodb table. Also it does not say any error so I can know what to do next.

I have an AWS account : a DynamoDB database with tables and a lambda with DynamoFullaccessDB role to the database.

I am trying to get an item from a table from the same account from DynamoDB ( i have made multiple tables , tried with string and number primary key )

The putItem function works and it insert the item in the specified table, but neither getItem nor DocumentClient get function do not work , as they don t return any error and do not return any item , although i specified the correct key and the correct table name.

Here is the code of the lambda function:

 var AWS = require('aws-sdk');
 AWS.config.update({region:'eu-central-1'});


exports.handler = async (event,context) => {
    // TODO implement
     // Create the DynamoDB service object
     AWS.config.update({region:'eu-central-1'});
     var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
      var params = {
  Key: {
   "testKey":{S: "sun"}
  }, 
  TableName: "test",
  ConsistentRead: true
 };
  var data =[];
 await ddb.getItem(params, function(err, data2) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);   
   data.push(err);
   data.push(data2);
 });


 const db = new AWS.DynamoDB.DocumentClient({
   region : 'eu-central-1' 
});

const params3 = {
  TableName : "test",
  Key: {
    testKey:"sun"
  }
};

  db.get(params3, (err, data4) => {
  if (err){
    console.log("Error:", err);
  } 
  else{
    console.log("Success:", data4.Item);

  } 
   data.push(data4.Item);
  console.log("Completed call");
});
         const response = {
        statusCode: 200,
        body: JSON.stringify(data)+JSON.stringify(" LUCHIAN ----") ,
    };
    return response;

};

enter image description here

enter image description here

enter image description here

enter image description here

1

1 Answers

1
votes

The issue is you are mixing syntax between async/await and callbacks, I'll assume you want to await a promise of getItem.

const AWS = require('aws-sdk')
AWS.config.update({ region: 'eu-central-1' })

const documentClient = new AWS.DynamoDB.DocumentClient()

exports.handler = async () => {
     const params = {
         Key: {
             testKey: { S: 'sun' },
         },
         TableName: 'test',
         ConsistentRead: true,
     }

     const item = await documentClient.getItem(params).promise()

     return {
         statusCode: 200,
         body: JSON.stringify(item) + JSON.stringify(' LUCHIAN ----'),
     }
 }