Pagination not working
Closed this issue · 2 comments
I've setup pagination as per the documentation, using the defualt configuration. The metadata section works fine, but all the items in the database show up in results
handler: (req, res) => {
Category
.find()
.select('-__v')
.exec((err, categories) => {
if (err) {
throw Boom.badRequest(err);
}
if (!categories.length) {
throw Boom.notFound('No categories found!');
}
res.paginate(categories, categories.length);
})
}
The response to /api/catgeories?limit=2&page=1
is
{
"meta": {
"count": 6,
"pageCount": 3,
"totalCount": 6,
"next": "http://localhost:6789/api/categories?limit=2&page=3",
"self": "http://localhost:6789/api/categories?limit=2&page=2",
"previous": "http://localhost:6789/api/categories?limit=2&page=1",
"first": "http://localhost:6789/api/categories?limit=2&page=1",
"last": "http://localhost:6789/api/categories?limit=2&page=3"
},
"results": [
{
"_id": "575169acdcb6202c0a13f2ef",
"name": "Entertainment",
"dateModified": "2016-06-03T11:27:04.227Z",
"dateCreated": "2016-06-03T11:27:04.227Z"
},
{
"_id": "575169d3dcb6202c0a13f2f0",
"name": "Utilities",
"dateModified": "2016-06-03T11:27:04.227Z",
"dateCreated": "2016-06-03T11:27:04.227Z"
},
{
"_id": "57516e3e60474f58228db8c2",
"name": "Religious",
"dateModified": "2016-06-03T11:45:28.010Z",
"dateCreated": "2016-06-03T11:45:28.010Z"
},
{
"_id": "57516e4460474f58228db8c3",
"name": "Services",
"dateModified": "2016-06-03T11:45:28.010Z",
"dateCreated": "2016-06-03T11:45:28.010Z"
},
{
"_id": "57516e4860474f58228db8c4",
"name": "Tickets",
"dateModified": "2016-06-03T11:45:28.010Z",
"dateCreated": "2016-06-03T11:45:28.010Z"
},
{
"_id": "57516e4f60474f58228db8c5",
"name": "Betting",
"dateModified": "2016-06-03T11:45:28.010Z",
"dateCreated": "2016-06-03T11:45:28.010Z"
}
]
}
As mentioned here #11:
The plugin can't assume what database or ORM you're using. It can't returns the results for you. It just takes care of the metadata.
So I guess in your case, you'll have to pass the page
and limit
parameters in some way to your find
method.
Edit: Looks like you're using Mongoose, you can check out the limit
and skip
method.
- http://mongoosejs.com/docs/api.html#query_Query-skip
- http://mongoosejs.com/docs/api.html#query_Query-limit
Maybe now that I think about it, it would be a good idea to expose a skip
parameter as well. Instead of computing it every time.
Cool. Thanks for the quick response.