0

I am using NodeJS 18 version, and I am trying to paginate the dynamo DB results using the async function, but it is not giving me any results. My dynamo DB table has the data.

I am using the dynamo db scan function to get 3 rows per iteration using the async.doWhilst function, but it returns nothing




export const handler = async (event) => {
    var startKey=[];
    var results = [];
    var pages = 0;
    await async.doWhilst(
        //iterators
        async (callback)=> {
            let params = {
                TableName: 'tb_notes',
                Limit: 1
            };
            
            if(!_.isEmpty(startKey)) {
                params.ExclusiveStartKey = startKey;
            }
            
            await docClient.scan(
                params,
                (err, data)=> {
                    if(err) {
                        console.log(err);
                        callback(err, {}); 
                    } else {
                        
                        if(typeof data.LastEvaluatedKey !== 'undefined') {
                            startKey = data.LastEvaluatedKey;
                        } else {
                            startKey = [];
                        }
                        
                        if(!_.isEmpty(data.Items)) {
                            results = _.union(results, data.Items);
                            
                        }
                        
                        pages++;
                        
                        callback(null, results);
                        console.log(data);
                    }
                }
            ).promise();
        },
        // truth test
        async (results, callback)=> {
            if(_.isEmpty(startKey)) {
                return false;
            } else {
                return true;
            }
            
        },
        //callback
         async (err, data)=>{
            if(err) {
                console.log(err);
            } else {
                console.log(data);  
            }
        }
    );
};


1 Answers1

2

You can solve your task with async/await only.

Refer to this answer to see How to use Async and Await with AWS SDK Javascript

Just scan until ExclusiveStartKey is falsy:

export const handler = async (event) => {
    try {
        const result = [];
        const params = {
            TableName: 'tb_notes',
            Limit: 1,
        };

        do {
            const { Items, LastEvaluatedKey } = await docClient.scan(params).promise();
            result.push(...Items); // push all scan result items to the result array
            params.ExclusiveStartKey = LastEvaluatedKey;
        } while (params.ExclusiveStartKey);

        console.log(result);

        // TODO: Call action to finish the lambda function
    } catch (error) {
        console.log(error);
    }
};

jarmod
  • 71,565
  • 16
  • 115
  • 122
hoangdv
  • 15,138
  • 4
  • 27
  • 48