发布时限制查询

Limit Query in publication

您好,我正在尝试实现自己的分页,但在尝试限制发布查询时出现以下错误。

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 [];
            }
        });

订阅码:

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');
    }

});

完整错误跟踪:

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"}}

您是否可以将 sort 添加到您的查找选项中(通常 limitsort 一起使用)。这就是我提出此建议的原因。

当您发布游标时,Meteor 将自动为该查询设置一个 observe,以便可以将更改发送给订阅者。然而,当查询被排序时,meteor 将自动设置一个 observeChanges 来代替。我认为这种差异是导致错误的原因。 Meteor 使用 'observe' 而不是 'observeChanges'。

我看到有关这种情况的一些问题已记录到 meteor(例如 #2766 and #1643),但我没有看到任何解决方案到位。

您要添加 sort 选项的另一个原因是 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

综上所述,我无法确定确切为什么你会收到错误,但鉴于上述情况,我猜这就是正在发生的事情。