javascript - 删除自动发布后,模板将循环访问用户集合

标签 javascript meteor spacebars

我已经为一个网站编写了代码,该网站允许您登录并与其他用户聊天。根页面有一个您可以聊天的用户列表。代码如下:

<!-- Top level template for the lobby page -->
<template name="lobby_page">
    {{> available_user_list}}
</template>

<!-- display a list of users -->
<template name="available_user_list">
    <h2 class="cha_heading">Choose someone to chat with:</h2>
    <div class="row">
        {{#each users}}
            {{> available_user}}
        {{/each}}
    </div>
</template>

<!-- display an individual user -->
<template name="available_user">
    <div class="col-md-2">
        <div class="user_avatar">
            {{#if isMyUser _id}} 
            <div class="bg-success">
                {{> avatar user=this shape="circle"}}
                <div class="user_name">{{getUsername _id}} (YOU)</div>
            </div>
            {{else}}
            <a href="/chat/{{_id}}">
                {{> avatar user=this shape="circle"}}
                <div class="user_name">{{getUsername _id}}</div>
            </a>
            {{/if}}
        </div>
    </div>
</template>

和辅助函数:

Template.available_user_list.helpers({
  users:function(){
    return Meteor.users.find();
  }
})
Template.available_user.helpers({
  getUsername:function(userId){
    user = Meteor.users.findOne({_id:userId});
    return user.username;
  }, 
  isMyUser:function(userId){
    if (userId == Meteor.userId()){
      return true;
    }
    else {
      return false;
    }
  }
})

我已经为聊天集合编写了发布/订阅代码,但这是当您单击其中一个用户并向他们发送消息时的情况。现在我已经删除了自动发布,在根页面中,用户看不到任何其他用户可以点击。有人可以帮忙解决这个问题吗?

最佳答案

如果自动发布被删除,Meteor.users 将仅发布当前用户的个人资料。

将以下发布添加到您的服务器代码:

Meteor.publish(null, function () {
    if (!this.userId) return this.ready();
    return Meteor.users.find({});
});

如果删除自动发布,则客户端将自动发布和自动订阅空发布。

此外,请记住仅包含那些不敏感的字段。对于,例如。您可能想省略密码字段或服务字段。

类似这样的事情:

Meteor.users.find({}, {
        fields: {
            profile : 1,
            emails  : 1
        }
    });

关于javascript - 删除自动发布后,模板将循环访问用户集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600141/

相关文章:

javascript - Datamap 的 D3 气泡不显示

javascript - 如何从 react 列表中删除单个项目

javascript - 单 View 页面永久链接为空,带有 Iron Router

meteor - 空格键:如何使用和/或在 if 语句中

javascript - 如何在空格键 #each 标签中显示来自两个不同集合文档的数据?

javascript - 反转 Canvas 上的路径

javascript - 多个 $http 的 Angular promise

mongodb - Meteor:我应该如何更新用户集合以在对象/字典中包含一个新属性?

javascript - Meteor Account 禁用网站创建帐户功能

javascript - 如何在空格键中使用动态对象属性名称?