'dynamodb scan method returning null

below is the code I'm trying to test by passing "type" property as "all". However, the returned data is null. The role set to this lambda is also given appropriate access to DB. There is data in the table as well.

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({ region: 'us-east-2', apiVersion: '2012-08-10' });
 
exports.handler = async (event, context, callback) => {
    // TODO implement
    const type = event.type;
    if(type === "all"){
        const params = {
            TableName: 'compare-yourself'
        };
        
        dynamodb.scan(params, function(err, data){
            if(err){
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                console.log(type);
                callback(null, data);
            }
        });
    } else if(type === "single") {
        console.log(type);
        callback(null, "Just my Data");
    } else {
        callback(null, "Hello from Lambda!");
    }
};


Solution 1:[1]

I added a promise resolve rejection against the scan function and it resolved with a null always. When I removed the same, it worked fine.

Solution 2:[2]

This question is old, but I recognize the code. It's from the Udemy course on AWS by Maximilian Schwarzmüller.

If you come here with the same question, to implement the DynamoDB scan function in that course today, with newer versions of nodejs, use a promise and place the dynamodb.scan in a try catch. This code is in the Q&A section of that course under the title "I'm not able to get data".

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') {
        const params = {
            TableName: 'compare-yourself'
        }
        try {
            const data = await dynamodb.scan(params).promise()
            callback(null, data)
        } catch(err) {
            callback(err);
        }
    } else if (type === 'single') {
        callback(null, 'Just my data');

    } else {
        callback(null, 'Hello from Lambda');
    }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 double-beep
Solution 2 alfredo