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

#1

//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);
})

#2

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:

#3

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

#4

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

#5

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})

#6

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?

#7

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

#8

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.

#9

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?

#10

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?

#11

Sorry i was talking about documents.

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

#12

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)?

#13

Yes it is users own document

There are no publications that are using Meteor.users

And i have removed autopublish too

#14

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);
});
#15

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

1 Like
#16

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