因此,在我的 server.js 中,我有以下代码来限制客户端接收的内容:
Meteor.publish('customerList', function()
{
return Meteor.users.find({roles: 'customer'}, {fields: {profile: 1}});
});
我只想使用 Roles 包查找“Angular 色”值为“客户”的用户。然后在 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>
那么当我现在看到此列表中的所有不同 Angular 色时,我做错了什么?如果我在订阅的 find()
中添加与发布的规则相同的规则,那么我们根本得不到任何结果。
最佳答案
因为有另一个出版物发布了 employee
,所以您只需要从 Meteor.users
中获取 customer
订阅回调,否则你也可能会得到一些 employee
。首先,将 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);
});
顺便说一句,通过获取
游标并将结果存储在 session 中,您将破坏 react 性。如果这是有意的——您只想要客户的一次性快照——您应该考虑在完成订阅后停止订阅,否则服务器将继续向客户端发送从未使用过的新客户:
var customerListSubscription = Meteor.subscribe('customerList', function()
{
var foundCustomers = Meteor.users.find({roles: 'customer'}).fetch();
Session.set('foundCustomers', foundCustomers);
customerListSubscription.stop();
});
如果您想要 react 性,请参阅 Kelly Copley 的回答。
关于javascript - meteor 发布/订阅困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23388326/