Hello I'm trying to implement my own pagination, but I'm getting the following error when trying to limit the publication query.
Exception from sub Products id Xbd4EW32oob8fBPkk Error: must use ordered observe (ie, 'addedBefore' instead of 'added') with skip or limit
Meteor.publish('Products', function(user, options) {
if (organization && organization.categories) {
//find products in array of categories
let products = Products.find({
categories: {
'$in': organization.categories
}
},
{limit: 10}
);
return products;
} else {
return [];
}
});
Subscription Code:
Router.route('/products', {
name: 'products',
loadingTemplate: 'loading',
waitOn: function() {
// return one handle, a function, or an array
return Meteor.subscribe('Products', {limit: 10});
},
data: function () {
return Products.find({});
},
action: function() {
this.render('ProductCatalog');
}
});
Full error trace:
I20170320-16:29:58.231(0)? Exception from sub Products id vDz4GLRXJ6s3cENeE Error: must use ordered observe (ie, 'addedBefore' instead of 'added') with skip or limit
I20170320-16:29:58.233(0)? at [object Object]._.extend.observeChanges (packages/minimongo/minimongo.js:325:13)
I20170320-16:29:58.234(0)? at [object Object].<anonymous> (packages/omknee:sales-process/sales-process.js:38:31)
I20170320-16:29:58.235(0)? at [object Object]._handler (packages/omknee:access-control/access-control.js:38:31)
I20170320-16:29:58.236(0)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1737:12)
I20170320-16:29:58.236(0)? at [object Object]._.extend._runHandler (packages/ddp-server/livedata_server.js:1035:17)
I20170320-16:29:58.237(0)? at [object Object]._.extend._startSubscription (packages/ddp-server/livedata_server.js:853:9)
I20170320-16:29:58.238(0)? at [object Object]._.extend.protocol_handlers.sub (packages/ddp-server/livedata_server.js:625:12)
I20170320-16:29:58.239(0)? at packages/ddp-server/livedata_server.js:559:43
I20170320-16:29:58.246(0)? Auth Strategy - Local. User : {"user":{"email":"laetitia.mendes@omknee.com"},"password":{"digest":"5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8","algorithm":"sha-256"}}
Is there anyway you can add sort
to your find options (typically limit
is used alongside sort
). Here is why I make this recommendation.
When you publish a cursor, Meteor will then automatically setup an observe
on that query so that changes can be sent down to the subscriber. However, when the query is sorted, meteor will automatically setup an observeChanges
instead. I think this difference is what is causing the error. Meteor used 'observe' instead of 'observeChanges'.
I see that some issues have been logged to meteor about this situation (e.g. #2766 and #1643) but I don't see that any resolution was put into place.
Another reason why you will want to add a sort
option is because Meteor won't be able to use the oplog for this query.
If your query has a limit but not a sort specifier, your query can't take advantage of oplog
With all that said, I can't pinpoint exactly why you are getting the error, but given the above, I'm guessing this is what is happening.