javascript - meteor 发布/订阅困惑

标签 javascript meteor

因此,在我的 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/

相关文章:

javascript - 从嵌套数组生成唯一组合 (JS)

javascript - 在 html 中引用 jquery 全局变量?

javascript - jquery如何从无序列表中选择项目

javascript - 将 CSV 文件读取为 HTML 下拉列表

javascript - Meteor.Error - 自定义[原因]

javascript - 打开 chrome.identity.launchWebAuthFlow 作为选项卡(Chrome 扩展)

javascript - Meteor:在 Blaze 中测试两个值相等(例如 {{#if someVar == 'someVal' }})的最佳方法是什么?

meteor - 如何在 Meteor.js 应用程序中获取服务器端的 http 请求 header 字段?

javascript - 从 json 文件中读取数据并插入到 mongodb (meteorjs)

node.js - 有没有办法在meteorjs上接受多部分表单?即图像上传?