【问题标题】:Return All Meteor Users Except The Current User?返回除当前用户之外的所有 Meteor 用户?
【发布时间】:2016-02-29 17:10:57
【问题描述】:

我有一个显示所有注册用户的页面,但想省略当前用户。有没有办法返回所有 Meteor 的用户除了当前用户。

这是我的html:

<template name="users">
    <div class="contentDiv">
        <div class="blueTop pageContent" id="profileName">Users</div>
            {{#each users}}
                <div class="pageContent text">
                    <a class="user link"  id="{{_id}}">{{profile.firstName}} {{profile.lastName}}</a>
                    <button class="addFriend">Add Friend</button>
                </div>
            {{/each}}
        </div>
    </div>    
</template>

还有我的javascript:

if (Meteor.isClient) {
    Meteor.subscribe("users");

    Template.users.helpers({
        users:function(){
            return Meteor.users.find({}, {sort: {firstName: -1}}).fetch();       
        }
    });
}


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

【问题讨论】:

    标签: javascript meteor publish-subscribe


    【解决方案1】:

    您可以使用比较查询运算符 $ne 过滤掉不等于指定值的文档,在您的情况下为 Meteor.userId()

    例如:

    Meteor.users.find({_id: {$ne: Meteor.userId()}});
    

    【讨论】:

    • 谢谢! @Matthias Eckhart
    • 我试图做到这一点,但当前用户仍然存在并回来了......
    • @webkit 请打开一个新问题并包含您的代码。我很乐意为您提供帮助。
    【解决方案2】:

    如果您使用的是出版物,您可以简单地使用$ne 运算符。 this.userId 在所有发布功能上设置为当前用户。

    Meteor.publish('all_users', function () {
      return Meteor.users.find({
        _id: { $ne: this.userId }
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2012-11-26
      • 2016-09-06
      • 2021-01-21
      相关资源
      最近更新 更多