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

二次信任 提交于 2019-11-30 18:20:42

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/19945225/listing-of-all-users-in-the-users-collection-not-working-first-time-with-meteor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!