[英]meteor publish/subscribe confusion
因此,在我的server.js中,我具有以下代码来限制客户端接收的内容:
Meteor.publish('customerList', function()
{
return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1}});
});
我只想使用Roles包查找值为“ customer”的“角色”的用户。 然后在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
});
当然,在我的模板中,我将显示以下这些值:
<template name="customer_search_result">
{{#each customers}}
<div>{{profile.firstname}} {{profile.lastname}}, {{profile.tel}}</div>
{{/each}}
</template>
那么,当我现在看到此列表中的所有不同角色时,我在做什么错呢? 如果我在订阅的find()
添加与发布时相同的规则,那么我们将一无所获。
您的发布和模板看起来不错,您只需要像这样更改您的订阅即可:
Meteor.subscribe('customerList');
然后,您需要这样的模板助手:
Template.customer_search_result.helpers({
customers: function(){
return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1}});
}
})
由于没有其他出版物它出版的employee
S,你需要采取只是customer
从s Meteor.users
在订阅回调,否则你可能会得到一些employee
S以及。 首先,将roles
添加到已发布的字段中(我认为这不是问题):
Meteor.publish('customerList', function()
{
return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1, roles: 1}});
});
然后更新订阅功能:
Meteor.subscribe('customerList', function()
{
var foundCustomers = Meteor.users.find({roles: 'customer'}).fetch();
Session.set('foundCustomers', foundCustomers);
});
顺便说一句,通过fetch
游标并将结果存储在会话中,您将破坏反应性。 如果这是有意的(您只希望获得客户的一次性快照),则应在完成订阅后考虑停止订阅,否则服务器将继续向从未使用过的客户发送新客户:
var customerListSubscription = Meteor.subscribe('customerList', function()
{
var foundCustomers = Meteor.users.find({roles: 'customer'}).fetch();
Session.set('foundCustomers', foundCustomers);
customerListSubscription.stop();
});
如果您需要反应性,请参阅凯利·科普利的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.