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