Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing of all users in the users collection not working first time with meteor js

Tags:

meteor

I am having the issue with listing all the user's in the users collection. When I take the listing page, only the currently logged in user's details are shown. But all users are getting listed once the page is refreshed and there on its fine.

On Server side, I have the following publish code

Meteor.publish("userList", function() {

    var user = Meteor.users.findOne({
        _id: this.userId
    });


    if (Roles.userIsInRole(user, ["admin"])) {
        return Meteor.users.find({}, {
            fields: {
                profile_name: 1,
                emails: 1,
                roles: 1,
                contact_info: 1
            }
        });
    }

    this.stop();
    return;
});

On Client side,

Meteor.subscribe('userList');

In the Template js file, I make the following call,

Meteor.users.find();

Please help me out with this issue. What am I missing here ?

like image 778
anoop Avatar asked Nov 19 '25 05:11

anoop


1 Answers

It sounds like a race condition with the subscription (it runs before the user is logged in). I'd recommend putting your subscription inside of an autorun:

Tracker.autorun(function() {
  if (Meteor.user()) {
    Meteor.subscribe('userList');
  }
});

This has the additional benefit of not starting your subscription before the user is logged in (saves resources).

BTW, I can't think of a reason why you'd need the this.stop() and the end of your publish function.

like image 152
David Weldon Avatar answered Nov 22 '25 04:11

David Weldon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!