Keystone paginate returns incorrect total and pages info, but correct 'results'?

477 views
Skip to first unread message

joes.mail...@gmail.com

unread,
Feb 28, 2016, 8:28:00 AM2/28/16
to Keystone JS
Hi,
 I am trying  to use the 'paginate' function to return a subset of results. 
 I am trying to do this on the basis of the following two examples:

 The snippet of API code I have written is as follows:


var keystone = require('keystone');
var async    = require('async');

var Order = keystone.list('Order');

exports
= module.exports = function (req, res) {
   
Order.paginate ({page: req.query.page || 1, perPage: 100})
       
.where ("application", req.params.id)
       
.exec (function (err, results) {
           
return res.apiResponse({
                success
: true,
                total
: results.total,
                length
: results.results.length
           
});
       
});
}
//Request
// https://myapp.com/api/application/56d18490709714717834e913
//Returns:
// {"success":true,"total":73,"length":20}

The total number of 'Order' objects is 73, and the total number of results where the Object has the specified 'application'  ID '56d18...' is 20.


However, the pagination is all based on the 'total', which is incorrect.  Basically the search 'filters' I am applying seem to be ignored by the paginate function and I don't know why.


Is there something I am doing wrong here?  Should the 'paginate' function be called somewhere else?


Thanks!

joes.mail...@gmail.com

unread,
Feb 28, 2016, 9:03:56 AM2/28/16
to Keystone JS
Ok it looks like the filter does indeed need to be included *in* the paginate function call, and not as a 'where' appended afterwards.  The following query works correctly:

exports = module.exports = function (req, res) {
   
Order.paginate ({
            page
: req.query.page || 1,

            perPage
: 5,
            filters
: {'application':req.params.id}

       
})
       
.exec (function (err, results) {
           
return res.apiResponse({
                success
: true,
                total
: results.total,
                length
: results.results.length
           
});
       
});
}

// {"success":true,"total":20,"length":20}

To be clear, the 'where' clause does work after the fact, but it is applied to the result of the original paginate query, which unless filtered queries the full contents of the model in question.  The result of that sort of query is the issue from my first post, where the total, and hence the logic for the results of 'paginate' (total, currentPage, pages, etc.) are incorrect, but the final 'results' list is correct.

Yao Li

unread,
Jul 14, 2017, 2:45:32 AM7/14/17
to Keystone JS
How to user filters for where('id').in(`${id_array}`)?

filters: {'id': [id_array]} not working

Bob Tian

unread,
Jul 14, 2017, 5:24:01 AM7/14/17
to Keystone JS
filters:{id:{$in:[id_array]}}
Reply all
Reply to author
Forward
0 new messages