Unable to get all userslist

In server

Meteor.publish(‘usersList1’,function(){
Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
})

In client

Template.allusers.onCreated(function() {
var self = this;
Meteor.subscribe(‘usersList1’);
self.autorun(function() {

});
});

var usrs = Meteor.users.find();

I am getting only logged in user record.

Please help me

Regards,
Jagan

If you only need all users in your template allusers you really should use template level subscription. No need for autorun.

Template.allusers.onCreated(function() {
 var self = this;
 self.subscribe('usersList1');
});

you can then access the users via a template helper

Template.allusers.helpers({
 allusers(){
  return Meteor.users.find();
 }
});

To make sure the subscription is ready, check for it in the template

<template name="allusers">
 {{#if Template.subscriptionsReady}}
  {{#each user in allusers}}
    ...
  {{/each}}
 {{else}}
  Loading ...
 {{/if}}
</template>

Thanks jamgold for your reply. I followed same steps, but still I am not getting the list of the users. For your information I have more fields in users profile collection, is this will give any problem for fetching?

More fields in the collection should not matter. The code I posted works for me, so maybe you need to share more code for people to help you.

You posted your publication like this:

Meteor.publish('usersList1',function(){
    Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});

If that is what your pub actually is, you aren’t returning anything. You are just running a query. You need to return the query for it to make it back to the client. Like so:

Meteor.publish('usersList1', function() {
    return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});

And then when you want to look at the users that you are currently subscribed to follow what @jamgold said.

Make sure you are using .fetch() so that you have an array of users instead of a cursor.

3 Likes

Damn, totally missed that :slight_smile: (even though I reflexively added it to my code snippet)

2 Likes

Thank you jpmoyn. I already used return statemtn. The issue resolved after I restarted the server by removing build folder in .meteor.
When I used console statements its not reflected the javascript change, dont know why?, so I restarted. Its working great now.

Thanks again jamgold