Slemgrim
Slemgrim

Reputation: 957

Get single value from meteor server to client

I have a setup route in which the first user creates his account. If he already has an account he has to be redirected to the index route.

On the serverside i would do it like this:

if(Meteor.users.find().count > 0){
  //do stuff
}

The users collection isn't published to the client. How can i return a simple true/false from the server to a route definition? I tried with Meteor.call('hasUser' function(result){ console.log(result); });

But since call is async on the client i always get 'undefined' in my result

Upvotes: 3

Views: 221

Answers (1)

David Weldon
David Weldon

Reputation: 64312

This has already been asked and answered here, however you could also do this with the publish-counts package:

$ meteor add tmeasday:publish-counts

server

Meteor.publish('userCount', function() {
  Counts.publish(this, 'userCount', Meteor.users.find());
});

client

Meteor.subscribe('userCount');
...
Counts.get('userCount');

In addition, it also comes with helpers like:

<p>There are {{getPublishedCount 'userCount'}} users</p>

Upvotes: 3

Related Questions