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 Answer 1

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

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

2 Comments

Why Limit: 1?
@jarmod To test our while loop with a small table, maybe.

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.