Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page, pagingCounter, hasPrevPage, hasNextPage, prevPage and nextPage don't work when using offset #54

Closed
LuigiPolidorio opened this issue Nov 19, 2019 · 1 comment

Comments

@LuigiPolidorio
Copy link

LuigiPolidorio commented Nov 19, 2019

When I try to paginate by offset:

const result = await this.paginate({}, {
    offset: 0,
    limit: 10
  }).catch(err => console.error('Caught:', err.message));

It's don't calculated the page, consequently the result parameters: pagingCounter, hasPrevPage, hasNextPage, prevPage, nextPage doesn't work.

Result:

totalDocs: 5570,
offset: 0,
limit: 10,
totalPages: 557,
page: undefined,
pagingCounter: NaN,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null 

Solution:
src/index.js
add this line above line 151:

page = Math.ceil((offset + 1) / limit);

Patch:

--- src/index.js
+++ src/index.js
@@ -150,6 +151,7 @@ function paginate(query, options, callback) {
 
       if (typeof offset !== 'undefined') {
         meta.offset = offset;
+        page = Math.ceil((offset + 1) / limit);
       }
 
       const pages = (limit > 0) ? (Math.ceil(count / limit) || 1) : null;
@LuigiPolidorio LuigiPolidorio changed the title Page and hasNextPage don't work when using offset Page, pagingCounter, hasPrevPage, hasNextPage, prevPage and nextPage don't work when using offset Nov 19, 2019
@aravindnc
Copy link
Owner

@LuigiPolidorio Thanks. Let me take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants