简体   繁体   中英

DynamoDB always returns “Response: null” on AWS Lambda

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:

在此处输入图像描述

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

What might be the problem here?

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:

  .....

}

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);
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM