Leonard Parisi
Leonard Parisi

Reputation: 200

Return All Meteor Users Except The Current User?

I have a page that displays all the registered users, but wanted to omit the current user. Is there a way to return all Meteor's users except the current user.

Here is my 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>

And my 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();
    });
}

Upvotes: 0

Views: 318

Answers (2)

corvid
corvid

Reputation: 11187

If you are using a publication, you can simply use the $ne operator. this.userId is set on all publication functions as the current user.

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

Upvotes: 0

Matthias A. Eckhart
Matthias A. Eckhart

Reputation: 5156

You could use the comparison query operator $ne to filter out documents which are not equal to a specified value, in your case Meteor.userId().

For example:

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

Upvotes: 1

Related Questions