user1118099
user1118099

Reputation: 113

How to know when user document loaded in Meteor Accounts

I understand that when writing code that depends on the collection being loaded into the client minimongo, that you should explicitly subscribe to the collection and pass in the appropriate callback for when it is finished loading.

My problem is that I store a lot of important subdocuments that my page needs to access in the users collection. I am using Meteor Accounts, and am trying to figure out a similar way to wait until the entire logged in user document is available. When using this to test:

console.log(Meteor.user());

the logged in case, it seems like it first registers an object with just the _id, and then sends the other fields later (I know I have to explicitly add other fields to publish from the server beyond email, etc.).

Is there a way for me to wait for the logged in user document to load completely before executing my code?

Thanks!

Upvotes: 2

Views: 906

Answers (1)

Tarang
Tarang

Reputation: 75955

Deps.autorun (previously Meteor.autorun) reruns when something reactive changes, which might fit your use case:

Client js

Deps.autorun(function () {
    if(Meteor.user() {
          //Collection available
      }
    });

If you're using a subscription you can also use its callback. Have a read about it on the docs as you might have to customize it a bit, and remove the autopublish package as well as get your other collections set up to subscriptions

Server js:

Meteor.publish("userdata", function () {
    //You might want to alter this depending on what you want to send down
    return Meteor.users.find({}, {}});
});

Client js

Meteor.subscribe("userdata", function() {
    //Collection available
});

Upvotes: 2

Related Questions