is there a way to wait until a collection is available until running the next line of code? Right now I am struggeling with Meteor.users not being available.
This is an easy code-example that shows how Meteor.users is NOT available. BUT when clicking the button it is available…
if (Meteor.isClient) {
// Those lines have problems finding Meteor.users and Meteor.user()!
console.log('AFTER A HOT PAGE RELOAD Meteor.user does IS NOT availabl');
console.log('Meteor.user()');
console.log(Meteor.user()); // NOT available: HOW do I wait with this call, UNTIL Meteor.users Collection is available?
console.log('Meteor.userId()');
console.log(Meteor.userId());
console.log('Meteor.users.find().count()');
console.log(Meteor.users.find().count()); // shows 0, although there are users
Template.hello.events({
'click button': function () {
// everything works fine when pressing the button
console.log('Meteor.user()');
console.log(Meteor.user());
console.log('Meteor.userId()');
console.log(Meteor.userId());
console.log('Meteor.users.find().count()');
console.log(Meteor.users.find().count());
}
});
}
I am using this with Flow-Router to secure an url. When the user does NOT have the right he is directed to home.
It works fine, but not on a page refresh…
doesCurrentUserHavePermission = function(permission) {
// this give me problems: on a page-refresh Meteor.user() returns undefined
// although the user is actually logged in and has the permission
var currentUser = Meteor.users.findOne({ _id: Meteor.userId() });
if (Roles.userIsInRole(currentUser, [permission]) ) {
return true;
}
return false;
};
LoggedInRoute.group({
triggersEnter: [
function(context, redirect) {
if (!doesCurrentUserHavePermission('permission')) {
FlowRouter.go('home');
}
}
]
});