【问题标题】:Meteor accounts-password, display emails of all userMeteor 账户-密码,显示所有用户的邮箱
【发布时间】:2016-11-28 00:44:28
【问题描述】:

我是 Meteor 的新手。出于学习目的,我只想列出所有用户的电子邮件。但只能看到登录用户的电子邮件。

注意事项:

  • 我正在使用 Meteor 1.4.2
  • 我在开发环境中。所以我 尚未删除“不安全”或“自动发布”。我知道这很糟糕 实践。但这只是一个快速入门。

我的模板助手看起来像:

Template.usersList.helpers({
    users () {
        return Meteor.users.find({}, {fields: {'username': 1, emails:1}});
    },
    email(){
        if(typeof this.emails === 'undefined'){
            console.error(this.emails, "Unauthorized Attempt");
            return "--NaN--";
        } else {
            return this.emails[0].address;
        }
    }
});

Blaze 模板:

<template name="usersList">
    This is Users List
    <ul>
        {{#each users}}
        <li>{{username}} | {{email}}</li>
        {{/each}}
    </ul>
</template>

结果显示所有用户的用户名。并且只显示登录用户的电子邮件。对于其他用户,“电子邮件”数组返回未定义。

jessica | dad@email.com
Waqas | --NaN--
Bob | --NaN--

在上面的结果中,jessica 是登录用户。看不到其他用户的电子邮件。

谁能告诉我如何为所有用户显示电子邮件。或者请指点我正确的方向?

谢谢, 艾哈迈德

【问题讨论】:

  • 你应该检查你的 email 助手中的 this 变量,看看它指的是什么。

标签: meteor meteor-accounts


【解决方案1】:

你可以在服务器上添加这个:

Meteor.publish("userData", function () {
  return Meteor.users.find();
});

这在客户端:

Meteor.subscribe("userData");

更多详情,请参阅documentation

请注意,您可能希望限制最终将实际发布给客户的内容。但我想你明白了。 Meteor.users 只是一个集合,因此您可以像从任何其他集合中一样从它发布。

【讨论】:

    【解决方案2】:

    刚刚发现我必须使用 pub/sub。所以,以下对我有用:

    //在服务器中

    Meteor.publish("userList", function () {
           return Meteor.users.find({}, {fields: {emails: 1}});
    });
    

    //在客户端

    Meteor.subscribe("userList");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多