By default, when a Meteor client starts up, it connects to the server and subscribes to documents in any Meteor.Collection
you defined. That takes some time to complete, since there s always some amount of delay in establishing the server connection and receiving documents.
Meteor.startup()
on the client is a lot like $()
in jQuery -- it runs its argument once the client DOM is ready. It does not wait for your client s collections to receive all their documents from the server. So the way you wrote the code, the call to find()
will always run too early and return 0.
If you want to wait to run code until after a collection is first downloaded from the server, you need to use Meteor.subscribe()
to explicitly subscribe to a collection. subscribe()
takes a callback that will run when the initial set of documents are on the client.
See:
meteor-publish
and meteor-subscribe