Hey guys! Out of nowhere today my pagination started going haywire. Pagination is super easy and the code is good (take a look?) but somehow the UI is not displaying the results.
I have a grid container 3 boxes wide. When I query, I skip the page number - 1, times the 3 grid boxes, and then limit the results by 3. I spent many hours today trying different methods and combinations of numbers and not a single query displayed correctly.
For example, with only 6 total items in for the results length, there should be 2 pages of results each with 3 grid boxes.
Somehow page 1 renders 3 boxes with results 1-3, that is good. However page 2 only returns item 4, and somehow on page 3 (which shouldn’t exist) it displays items 5 and 6.
Page_____Amount to Skip____Items Returned______Correct?
_1_____________0_____________1,2,3____________Yes
_2_____________3_____________4_______________No, should contain 5,6
_3_____________6_____________5,6_____________No, should be empty
One example, but none work. Am I going crazy? Or do you seen something wrong that I can’t? Thanks!
Moralis.Cloud.define("renderMyProjects", async (request) => {
const rowsSkipped = ( (request.params.page - 1) * 3);
const currentUser = request.user;
const relation = currentUser.relation('projects');
const query = relation.query();
query.descending('createdAt');
query.skip(rowsSkipped);
query.limit(3);
query.select('creatorProfilePic', 'title','projectPhoto', 'summary', 'isVerified','date','description', 'creator.username', 'creator.profilePic', 'createdAt');
const queryResults = await query.find({useMasterKey:true});
const results = [];
for (let i = 0; i < queryResults.length; ++i) {
results.push({
"username": queryResults[i].attributes.creator.attributes.username,
"profilePic": queryResults[i].attributes.creator.attributes.profilePic._url,
"title": queryResults[i].attributes.title,
"projectPhoto": queryResults[i].attributes.projectPhoto._url,
"summary": queryResults[i].attributes.summary,
"createdOn": queryResults[i].attributes.date,
"description": queryResults[i].attributes.description,
'isVerified': queryResults[i].attributes.isVerified,
});
}
return results;
});