0

Consider the code :

const AWS = require('aws-sdk');
const dynamoDB = new AWS.DynamoDB({
    region : 'us-east-2'  , 
    apiVersion: '2012-08-10'
});


exports.handler = async (event , context , callback) => {
    const type = event.type;
    if (type === 'all') { 
      // Which table we want to scan
      const params = {
          TableName : 'compare-yourself'
      };
      await dynamoDB.scan(params ,(err , data) => {
         if (err) {
             callback(err);
         }
         else{
             callback(null , data);
         }
         
      });
        // callback(null , "All Data");
    }
    else if (type === 'single') {
        callback(null , "Single data");
    }
    else{
        callback(null , "Everything else...");    
    }
    
};

When I test this piece of code on AWS Lambda with the value :

enter image description here

The result is always "Response: null" , even through there is data in the DynamoDB table compare-yourself.

What might be the problem here ?

1
  • 1
    Why are you doing await and also at the same time waiting for callback in dynamoDB.scan ? Commented Jul 25, 2020 at 11:21

3 Answers 3

2

Promise will solve the issue. I prefer documentclient.


const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event, context, callback) => {
    const type = event.type;
    if (type === 'all') {
        const params = {
            TableName: 'compare-yourself'
        };
        let data = await documentClient.scan(params).promise();
        callback(null, data);
    } else if (type === 'single') {
        callback(null, "Single data");
    } else {
        callback(null, "Everything else...");
    }
};

The data will be an object with table values in the Items,

{
 Items: [],
 Count:
 ScannedCount:

  .....

}
Sign up to request clarification or add additional context in comments.

Comments

1

try use DocumentClient();

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

const params = {
          TableName : 'compare-yourself'
      };

documentClient.scan(params, function(err, data) {
   if (err) console.log(err);
   else console.log(data);
});

Comments

1

Mistake:

The problem is you are doing both callback and await at the same.

Fix:

So you could put the scan function in a try block, so on error we do callback error in the catch block and stop the function without proceeding further (return) else you will send the data.

Code:

const AWS = require('aws-sdk');
const dynamoDB = new AWS.DynamoDB({
    region : 'us-east-2'  , 
    apiVersion: '2012-08-10'
});


exports.handler = async (event , context , callback) => {
    const type = event.type;
    if (type === 'all') { 
      // Which table we want to scan
      const params = {
          TableName : 'compare-yourself'
      };
      
      try {
        await dynamoDB.scan(params).promise();
      } catch (e) {
        callback(e);
        return;
      }
      callback(null , data);
    }

    else if (type === 'single') {
        callback(null , "Single data");
    }
    else{
        callback(null , "Everything else...");    
    }
    
};

Note:

Just wanted to add this, when performing scan be aware of the limits, if the limit exceeds you won't get the entire entries in the table.

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.