Subscribe returns users which are not part of publish


#1

I am trying to fetch users based on query, subscribe method returns user which is not part of publish. If my publish returning 5 users, on client subscribe returning 5+1 user. Along with the published users subscribe returning additional user, in this case the additional user is logged in user.

Please find my publish and subscribe methods below.

Publish Method.

Meteor.publish('allUsersPublish', function(){
	var userInfo = Meteor.users.findOne({_id: this.userId});
	var query = {};
	if(this.userId){
		if(userInfo.profile.isOrgAdmin){
			query = {
				"profile.orgId" : this.userId
			}
		}else{
			query = {
				"createdBy" : this.userId
			}
		}
	}
  return Meteor.users.find(query);
});

Subscribe method in router.

Router.route('/user',{
  name : 'user.list',
  layoutTemplate : 'layoutAfterAuth',
  waitOn: function() {
      return Meteor.subscribe('allUsersPublish');
  },
  data : function(){
      return {
       userListData : Meteor.users.find({} , { sort: {createdAt: -1}})
      };
  },
  action : function (){
        this.render('userListPage');
  }
});

#2

And where is the problem ?
Logged in user is published even without declaring first publish function.


#3

I am new to meteor, as per my understanding subscribe will look in the documents which are published. In this case current logged in user in not in the published documents. After subscribing if i search for users using Meteor.users.find() it will search in the published documents based on the query, why is it returning current logged in user document though it is not part of the published documents?


#4

From the docs:

Like all Mongo.Collections, you can access all documents on the server, but only those specifically published by the server are available on the client.

By default, the current user’s username, emails and profile are published to the client.


#5

Got It. Thanks for info.