Hi, i will need to get a count of users, in before action section
Router.onBeforeAction(function() {
if (! Meteor.userId()) {
let firstBoot = Meteor.users.find({}).length
console.log(firstBoot)
firstBoot == 0 ? this.render('register') : this.render('login')
} else {
this.next();
}
});
but firstBoot is undefined, i can try to use the Meteor.methods in my server code, and get user count in router using Meteor.call? 10Q 
try either Meteor.users.find().count()
or Meteor.users.find().fetch().length
collection.find()
returns a cursor, therefore length is undefined. count()
counts a cursor or fetch()
converts the resultset into an array.
Edit: you also need to have your users collection fully published in order to receive that count on the client.
i try it, count() method returned 0, but users in collection > 0
See my edit. You need to have the whole user collection published to be able to count on the client.
You have other options:
- Publish a safe subset of the user information.
- Publish just the counts
- Calculate the count at the server and retreive via method
end
5
Meteor.users.find().count() works in my code. I am using the accounts-password package.
in onBeforeAction on iron router? when you are loggen in
end
7
I believe this hits the spot. So, using the tmeasday:publish-counts package
, you can do the following.
Meteor.publish('userCount', function() {
Counts.publish(this, 'user-count', Meteor.users.find());
});
Then, in your home or β/β route
waitOn: function() {
return [Meteor.subscribe('userCount')];
}
Then, your code becomes
Router.onBeforeAction(function() {
if (! Meteor.userId()) {
let firstBoot = Counts.get('user-count');
console.log(firstBoot)
firstBoot == 0 ? this.render('register') : this.render('login')
} else {
this.next();
}
});
This way, you do not have to publish the entire/part of βuserβ collection and only publish the information you need (the user count).
2 Likes
thanks for this answer, it works for me
But what are you trying to do?
if application has not user, show register template