Accessing more data via Meteor.user() in a method

Hi guys,

Long time no see! Hope everyone is doing fine in these crazy times!

Following situation:
I have some data coming from another collection which I need in a lot of my method invocations. We already have the Meteor.user() helper on the server which we can use inside any Meteor method to access the loggedin user who is currently invoking that method.
Question is: Is there any way to add any data to this helper? Meaning, I would like to extend that returned user somehow:

  async testMethod() {
    const user = Meteor.user(); // == user currently invoking that method
    const someDataFromAnotherCollection = user.moredata; // user.moredata was set somewhere else - any way to do that?

Can someone help?
Thanks guys!

best, Patrick

For my liking, the Meteor.user() is already a bit too much sugar as it obfuscates what is actually happening in the code. To me it is much clearer to use Meteor.userId() to simply get the id of the logged in user at the start of the method and then on the following lines just perform the Collection.findOne queries that you need with that id. Anyone else reading that code will then clearly see what data is coming from where.

And as a side benefit, you can then also limit the fields that are returned about your user. With Meteor.user() you are likely asking the db for a lot more data than you actually need.

1 Like

Fair enough, I get your point.
In my case the codebase is already huge and the additional data I need now is because of a refactoring. Being able to use the Meteor.user() helper would be of great help!

Then perhaps you could consider just separating those queries to its own function, something like userWithAdditionalData(), and just importing that where necessary.

Overloading Meteor.user() itself with extra data would not only be hard to read, but downright misleading when a new dev comes and just reads Meteor docs about its functionality.

But I do get your position. Stradling the line between maintainable code and just gettings things done is often rather tricky. And I’m certainly no stranger to shortcuts myself, let’s put it like that :slight_smile:

1 Like

I share the views as expressed by @vooteles: it would be best to refrain from using opaque tricks to enhance Meteor.user(). Just as he suggested, add a new function (to be accessible from your methods) where you compose the data from the Users collection and those other documents you mentioned.

However, you also wrote “I need in a lot of my method invocations”. If the frequency with which those methods will be called is relatively high, with this you may introduce a significant overhead and thus a performance penalty.

If the user data (including what’s in the second collection) doesn’t change frequently, AND you can live with some staleness of said data, consider caching. If you have just a single Meteor instance and your app will never grow beyond, some javascript base cache will do it (e.g. js-cache). Otherwise, if your app is likely to run in multiple instances, redis works great as a distributed cache.

1 Like