import query from '/imports/api/groupMembers/query/membersOfaGroup.js';
.
.
.
export default withQuery(
props => {
var limit = Session.get('limit');
var skip = Session.get('skip');
var page = Session.get('page');
var groupId = localStorage.getItem("groupId");
if (!limit)
{
limit = 2;
Session.setDefault('limit', limit)
}
if (!skip)
{
skip = 0;
Session.setDefault('skip', skip)
}
if (!page)
{
page = 0
Session.setDefault('page', page)
}
return query.clone({
groupId:groupId,
limit:limit,
skip:skip
});
},
{
reactive: true,
})(Members);
Where obviously Members is my class.
First time around limit = 2, skip = 0, I get back the first userId and groupId as expected
Second time around when I click next to go to the next page where limit is still 2 but skip is now 2, I still get the same userId and groupId as in the first page, as if limit is also limiting the records server side instead limiting what comes back to me but doing the skipping on the database where all records exist. So I was expecting the skipping to be done database side but limiting to be done client side when the extraction has happened. For example if there is 12 records and we are on page 2 going to page 3 we would skip 4 records extract the rest but only display 2 records, as we limit what we see to 2 records, something like that.
Why does it not work for me? What am I missing? Have you tried grapher pagination before. Did it work for you? Are you willing to maybe try it and tell me if it works for you and also tell me what you did differently? Please help. Thank you.
B.
Thank you so much for your reply.
Please change my code above to demonstrate exactly what you mean. I have done the following and it does not yield the result I want:
export default withQuery(
props => {
var limit = Session.get('limit');
var skip = Session.get('skip');
var page = Session.get('page');
var groupId = localStorage.getItem("groupId");
// Session is a global object. Try to work with local variables and pass them through your props. In this case, limit, skip and page
// should be in the local state of your view that needs to be paginated. You may
// initialize an object as { limit: 2, skip: 0, page: 0 } to have some defaults. Otherwise ... you need to write a lot of code and ifs or switches.
if (!limit)
{
limit = 2;
Session.setDefault('limit', limit)
}
if (!skip)
{
skip = 0;
Session.setDefault('skip', skip)
}
if (!page)
{
page = 0
Session.setDefault('page', page)
}
return query.clone({
groupId:groupId,
limit:limit,
skip:skip
}); // Just to make sure first that you get your data, try to make it non-reactive and do a fetch() here and console log it. Also try with hard coded limit, skip and page.
},
{
reactive: true,
})(Members);
Ok, I was saying that you could let go the entire tracker and client part and work it out on the server by fetching your data there (do fetch() for your query). In this way you take away one possible area of failure. Also try working with hard coded limit and sort numbers just to make sure you get them right. Then you can channels your limit and skip data from the client.
Whenever you use limit, you need to use sort as well for a field which you are sure it is being returned and whenever you search/query for a field you need to have a DB index created for that field in the sort option that you are planning to use.
E.g.
Groups._ensureIndex({ userId: 1, createdAt: -1 }) // where Groups is a Mongo Collection
Okay, I got it. I was supposed to use the $postOptions because when you clone the query some of the “options” that I passed are lost. I found the solution in this post. Had to do some reading and putting 2 and 2 together though. Here is the $postOptions:
As per the same query I must move away from using global query and use named query instead. So I must figure out how to write a named query equivalent of the same query.