How to wait for the Meteor.users collection gets ready in a library?

I had many other standard and custom fields in Meteor.users, but it didn’t help.
I didn’t try the rest of your suggestions, because I’ve just migrated to custom collection and that works fine. Thanks for your help

I definitely need to study how fastrender works in detail. My publication was pretty similar to your sample null publication, even if it’s still not completely clear ─ reading the documentation I found on github ─ what other changes are necessary in order to make fastrender working properly in my application. I made a quick test but nothing changed ─ probably because I forgot something. Need to spend more time on that. Thanks

Fast render works in 2 ways… First is it gathers your automatic publications (the default user as well as any specified using a null publication) and injects that data into the initial html of the page which is then added to the proper collections when the page is loaded. Second and in addition to the first, it work along side Iron Router or Flow Router and figures out the subscriptions for the route and sends down the data that would normally need to be waited on before the page for that route could be rendered.

If you have a public repository, I’d be more than willing to take a look.

2 Likes

Do you mean that it sends the data directly to Minimongo before the page is rendered?

Thanks for the offer. At the moment everything is on a private repository, but maybe in the future I’ll open the source code.

Sort of… What precisely happens is fast-render injects a script tag, with a type of text/fast-render, containing that data in the head of the inital document. Once the page is loaded this tag is parsed and the relevant data is inserted into each collection so that by the time the rendering happens for the page all necessary data is available.

1 Like

As @copleykj said, it is inserted into the HTML.
You can see it in action if you look at the source code of your app once you add fast-render.
Even if you don’t configure fast-render, it will be used for the user profile and you can see that in the code. It looks like this:

I am fairly new to Meteor so I am not sure if this is accurate:
In the accounts-base package in the accounts_common.js file, we see where the “users” collection is defined. Realize that “autopublish” is set to false.
@see https://github.com/meteor/meteor/blob/master/packages/accounts-base/accounts_common.js#L141

Meteor.users = new Mongo.Collection("users", {
  _preventAutopublish: true,
  connection: Meteor.isClient ? Accounts.connection : Meteor.connection
});

If your code is executing on the CLIENT side for the first time, when the code to publish specific user fields (and hide other fields) has not been executed on the server (Code to autopublish this is in accounts_server.js), then you always get undefined; essentiall, a call to Meteor.user() executes the following:

Meteor.users.findOne(userId)

If you execute this on your javascript console with the correct userId, you always get undefined because users collection has not yet been published (not until it is published when it executes on the server)

The reason Meteor.userId() exists is because the userId is set on the connection
@see https://github.com/meteor/meteor/blob/master/packages/accounts-base/accounts_client.js#L11

Meteor.userId = function () {
  return Accounts.connection.userId();
};
1 Like