Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 418
Slemgrim Avatar asked Dec 05 '25 12:12

Slemgrim


1 Answers

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>
like image 138
David Weldon Avatar answered Dec 08 '25 01:12

David Weldon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!