问题
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