Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor - remove current user from list of users

Tags:

meteor

I'm currently publishing a list of users within a group. Although this works I'm unable to prevent the current user from appearing in the list. How do I remove the current user. Thanks in advance for any suggestions and help.

Path: publish.js

Meteor.publish('studentList', function (group) {
  if (Roles.userIsInRole(this.userId, ['teacher'], group)) {

  return Meteor.users.find({roles:'student'}, {fields:{emails:1, profile: 1, roles: 1}});

  } else {

    // user not authorized. do not publish secrets
    this.stop();
    return;

  }
});

Path: studentList.js

Template.studentList.onCreated(function() {
    var self = this;
    self.autorun(function() {
        self.subscribe('studentList')
    });
});

Template.studentList.helpers({
    studentList: ()=> { 
        return Meteor.users.find({}); 
    },
});
like image 966
bp123 Avatar asked Oct 31 '25 16:10

bp123


2 Answers

In your client side code you can exclude users whose user id matches the current user:

Template.studentList.helpers({
    studentList: ()=> { 
        return Meteor.users.find({_id: { $ne: Meteor.userId() }}); 
    },
});

Or you could remove it from the result on the client side by doing Meteor.users.find({}).fetch().someRemoveLogic().

like image 187
Brett McLain Avatar answered Nov 03 '25 18:11

Brett McLain


By default, logged in user's data is subscribed to the client side. There's no way of removing that subscription. Because under the hood Meteor.user() works from this subscription. If you need to exclude it from client side helper, you can make a query that excludes current user's data. An example would be like this

Meteor.users.find({_id: { $ne: Meteor.userId()}}); 
like image 23
Faysal Ahmed Avatar answered Nov 03 '25 18:11

Faysal Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!