Find query not working while using publication and subscription(my query is not returning the desire collection

//my publication code

if (Meteor.isServer){
Meteor.publish(‘usersPub’,function(){
//console.log(this.userId);
var options = {
fields: {
‘profile.adminid’: this.userId
}
}
return Users.find(options);

})
}

// subscription code
this.usersTracker = Tracker.autorun(()=>{
const Users = Meteor.users;
Meteor.subscribe(‘usersPub’);
const users = Users.find().fetch();
this.setState({users})
console.log(“New Users”,users);
})

I’m assuming you have set Users = Meteor.users somewhere in your publication to match your client autorun?

Your issue is probably that you have not specified the query in the publication’s find. For example: return Users.find({}, options);

You should also move static references out of your autorun. These only need declaring at the top, since they aren’t affected by re-running of the autorun (you have no reactive variables in your subscribe):

const Users = Meteor.users;
Meteor.subscribe('usersPub');
this.usersTracker = Tracker.autorun(() => {
  const users = Users.find().fetch();
  this.setState({ users });
  console.log('New Users', users);
});

BTW. It would help us in reviewing your code if you enclosed it in triple-backticks:

```
like
this
```

Thanks :slight_smile:

i have modified my publication

const Users = Meteor.users;

if (Meteor.isServer){
  Meteor.publish('usersPub',function(){
       return Users.find({'profile.adminid': this.userId});
  })
}

this query is working fine in console

db.users.find({‘profile.adminid’:‘4vXBT4keLTbhcxsnq’})

what i really want is to pick those collections that contains profile.adminid’: this.userId this

Hope you understand

Are you saying that db.users.find({'profile.adminid':'4vXBT4keLTbhcxsnq'}) works, but return Users.find({'profile.adminid': this.userId}); does not?

this works on console db.users.find({'profile.adminid':'4vXBT4keLTbhcxsnq'})

when I write this query on publication function it doesn’t works

Users.find({‘profile.adminid’: this.userId})

Modify your publication to log the results. Maybe something like:

Meteor.publish('usersPub',function(){
  const cursor = Users.find({'profile.adminid': this.userId});
  console.log(cursor.fetch());
  return cursor;
})

Do you see the expected documents?

If not, what do you see?

Well the log is showing correct collection. But on the client side i am getting every collection.

So it’s working, but not as you want.

You will need to add the options parameter to the find. See also The Meteor Guide.

Dear on the client i am getting the the right collections plus the collection of admin to who is creating users. why is this so?

When you say collection, do you mean document or do you mean field?

A collection contains documents. A document contains fields.

Perhaps you could paste an example of what you get and an example of what you want?

Sorry i was talking about documents.

I wonder why this admin document is showing on the client side

Is that admin document the user’s own document?

Do you have other publications using Meteor.users which you also subscribe to?

Do you have autopublish installed (you need to remove this)?

Yes it is users own document

There are no publications that are using Meteor.users

And i have removed autopublish too

In that case, you should ensure the find() on your client matches (as closely as possible) the find() on the server.

this.usersTracker = Tracker.autorun(() => {
  const users = Users.find({'profile.adminid': Meteor.userId()}).fetch();
  this.setState({ users });
  console.log('New Users', users);
});

Its working fine now thanks a lot for the your time dear.

1 Like

In general terms, the find on the client doesn’t need to be exactly mimicking the one on the server.

When you publish a collection from the server and there are no other publications on the same collection, the local minimongo cache will only contain the documents published by the server, that means that a normal Collection.find() will yield the correct result.

This, sadly, does not apply to the User collection. When you log-in, the user document for the current user is automatically published and stored in your client-side minimongo cache.

That’s why you’re pickiing it up in your User.find().
Essentially, your code should work if you just “exclude” the current user from your query on the client, without having to replicate the query clause you made on the server:

Users.find({_id: { $ne: Meteor.userId() }}).fetch()
1 Like