发布中限制查询

时间:2017-03-20 14:39:52

标签: meteor publish-subscribe meteor-blaze

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

  

子产品ID Xbd4EW32oob8fBPkk的异常错误:必须使用   有序观察(即,添加之前'而不是'添加')跳过或   限制

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

1 个答案:

答案 0 :(得分:1)

无论如何,您可以在查找选项中添加sort(通常limitsort一起使用)。这就是我提出此建议的原因。

当您发布游标时,Meteor将自动在该查询上设置observe,以便可以将更改发送给订阅者。但是,当查询排序时,meteor将自动设置observeChanges。我认为这种差异是造成错误的原因。流星使用&#39;观察&#39;而不是观察变化&#39;。

我发现meteor已经记录了一些有关此情况的问题(例如#2766#1643),但我没有看到任何解决方案已经落实到位。

您希望添加sort选项的另一个原因是Meteor won't be able to use the oplog for this query

  

如果您的查询有限制但没有排序说明符,则您的查询无法利用oplog

尽管如此,我无法确定完全为什么会出现错误,但鉴于上述情况,我猜测这就是发生的事情。