简体   繁体   English

流星发布/订阅混乱

[英]meteor publish/subscribe confusion

So, in my server.js I have the following code to limit what the client receives: 因此,在我的server.js中,我具有以下代码来限制客户端接收的内容:

Meteor.publish('customerList', function()
{
    return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1}});
});

I only want to find users with 'roles' of value 'customer', using the Roles package. 我只想使用Roles包查找值为“ customer”的“角色”的用户。 Then on the client.js I do ANOTHER find() in a subscribe: 然后在client.js上,我在订阅中执行另一个find()

Meteor.subscribe('customerList', function()
{
    var foundCustomers = Meteor.users.find().fetch();

    Session.set('foundCustomers', foundCustomers); //i have a Session.get elsewhere which returns this cursor
});

And ofcourse in my template I show these values like this: 当然,在我的模板中,我将显示以下这些值:

<template name="customer_search_result">
    {{#each customers}}
        <div>{{profile.firstname}} {{profile.lastname}}, {{profile.tel}}</div>
    {{/each}}
</template>

So what am I doing wrong when I am seeing all of the different roles in this list now? 那么,当我现在看到此列表中的所有不同角色时,我在做什么错呢? If I add the same rules in my subscribed find() as in my published then we get no result at all. 如果我在订阅的find()添加与发布时相同的规则,那么我们将一无所获。

Your publish and template look fine, you just need to change your subscribe like this: 您的发布和模板看起来不错,您只需要像这样更改您的订阅即可:

Meteor.subscribe('customerList');

Then you need a template helper like so: 然后,您需要这样的模板助手:

Template.customer_search_result.helpers({
    customers: function(){
        return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1}});
    }
})

Since there is another publication which publishes the employee s, you'll need to take just the customer s from Meteor.users in the subscribe callback, otherwise you might get some employee s as well. 由于没有其他出版物它出版的employee S,你需要采取只是customer从s Meteor.users在订阅回调,否则你可能会得到一些employee S以及。 First, add roles to the published fields (I assume this isn't a problem): 首先,将roles添加到已发布的字段中(我认为这不是问题):

Meteor.publish('customerList', function()
{
    return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1, roles: 1}});
});

Then update the subscribe function: 然后更新订阅功能:

Meteor.subscribe('customerList', function()
{
    var foundCustomers = Meteor.users.find({roles: 'customer'}).fetch();
    Session.set('foundCustomers', foundCustomers);
});

By the way, by fetch ing the cursor and storing the results in the session, you will break reactivity. 顺便说一句,通过fetch游标并将结果存储在会话中,您将破坏反应性。 If this is intentional - you just want a once-off snapshot of the customers - you should consider stopping the subscription after you're done with it, otherwise the server will keep sending new customers to the client which are never used: 如果这是有意的(您只希望获得客户的一次性快照),则应在完成订阅后考虑停止订阅,否则服务器将继续向从未使用过的客户发送新客户:

var customerListSubscription = Meteor.subscribe('customerList', function()
{
    var foundCustomers = Meteor.users.find({roles: 'customer'}).fetch();
    Session.set('foundCustomers', foundCustomers);
    customerListSubscription.stop();
});

If you want reactivity, see Kelly Copley's answer. 如果您需要反应性,请参阅凯利·科普利的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM