How do you add extra data in currentUser object?

Asked in http://stackoverflow.com/questions/36375191/how-do-you-add-extra-data-in-currentuser-object

I am trying to add an extra information that is not in profile for the currentUser object in Meteor. I am thinking it is possible and the technique should be somewhere in meteor/alanning:roles.

e.g. if I have a organizations object in users e.g.

{ 
  _id: ...
  profile: { ... }
  roles: [ ... ]
  organizations: [ ... ]
}

I would like to see it when I do

{{ currentUser }}

or in AngularJS

{{ currentUser | json }}

You could create a publish and specify the field you want to use.

Meteor.publish("extraUserData", function() {
    return Meteor.users.find(this.userId, {
        fields: {
            'someExtraField': 1
        }
    });
});

As @krizzli already said, you can publish additional fields (according to Meteor docs, only the profile dictionary is published by default).

If you don’t want to publish the fields, because you don’t always need them and want to avoid unnecessary network traffic, you can also use methods to set and get data. I’m using a mixture of both approaches in my app.

Problem with that approach is it requires me to subscribe to the extra user data explicitly. From alanning:roles, I don’t think I had to explicitly subscribe and it was already in my object.

The reason why I am trying to do it with the alanning:roles approach is that Meteor.users.findFromPublication would result in having only one user (the current one) show up.

I may have fixed the issues with FindFromPublication with my patch, still waiting for approval on the PR to confirm. Anyway, I think I would also need to have a resolve block to wait for the subscription containing the roles and other extra data on my user to resolve before going on. Not a big deal.

Pro tip: use a null publication so you can automatically add extra data.

Meteor.publish(null, function () {
  return Meteor.users.find(this.userId, {
    fields: {
      // do whatever you gotta do here
    }
  })
});

I already have, an I get a timing issue when the publish does not get there in time. Hence I am making it into a subscription that I can wait to ensure it is ready before going forward.

It could also be a 1.3 thing. I wonder if anyone had issues where this.userId is undefined in the publish function.

Oh… derpy…

This works

Meteor.publish('user-extra', function() {
  console.log(this.userId)
  return Meteor.users.find(this.userId, {
    fields: {
      roles: true,
      venues: true
    }
  })
})

This does not

Meteor.publish('user-extra', () => {
  console.log(this.userId)
  return Meteor.users.find(this.userId, {
    fields: {
      roles: true,
      venues: true
    }
  })
})

I wanted to shorten it to this too

Meteor.publish('user-extra', () => Meteor.users.find(this.userId, {
    fields: {
      roles: true,
      venues: true
    }
  }));

You can’t use the fat arrow notation for publications. The “in-publication” context has to be set (it can’t be the enclosing context).

Edit: The guide says:

Note: Since we need to access context on this we need to use the function() {} form for publications rather than the ES2015 () => {}. You can disable the arrow function linting rule for publication files with eslint-disable prefer-arrow-callback. A future version of the publication API will work more nicely with ES2015.

1 Like

The => syntax won’t work here since it uses the surrrounding context for this. Beware: The arrow syntax is not just a short-cut for a regular closure.

2 Likes