返回除当前用户之外的所有流星用户

Return All Meteor Users Except The Current User?

本文关键字:用户 流星 返回      更新时间:2023-09-26

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

这是我的网页:

<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();
    });
}
您可以使用

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

例如:

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

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

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