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({});
},
});
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().
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()}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With