Skip to content Skip to sidebar Skip to footer

Make Meteor Reactive To Specific Subitem Of Meteor.user()

What I'm Trying to Do... I need to use some subproperties which are stored in the user's Meteor.user() object, such as Meteor.user().profile.preferences.preference_one, Meteor.user

Solution 1:

I don't know if this is the best way, but have a look at this example:

Tracker.autorun(function() {
  var user = Meteor.user();
  if (user && user.profile)
    Session.set('p1', user.profile.preference1);
});

Tracker.autorun(function() {
  var p1 = Session.get('p1');
  console.log("p1 is " + p1);
});

The first autorun will fire every time the user data changes, however the second autorun will fire only when that particular property changes.

Solution 2:

David's solution is great (as always).

Just to offer some variety, I'd suggest moving your preferences (or the whole darn profile) to its own collection. Then, use a .publish(null,... to always have access to that collection.

Either solution will work great, it is simply my preference to have nothing except login credentials attached to the critical users collection.

Post a Comment for "Make Meteor Reactive To Specific Subitem Of Meteor.user()"