Following on from a reply by @veered in another thread I wanted to share a tip to new Meteor developers but didn’t want to hijack that thread.
This is very true, over-use of Meteor.user()
in template render functions can make the template overly reactive.
What can be worse, if you store lots of data on the user object (like I do) then using Meteor.user()
on the server can be a very expensive db operation when often only a single field is required.
That is why I patch Meteor.user()
to only return the required fields:
// Patch Meteor.user to allow filtering of fields to be more db efficient & less reactive
Meteor.user = function(fields) {
if (Meteor.isDevelopment && !fields) console.warn('Meteor.user() called with unlimited fields');
const userId = Meteor.userId();
return userId ? Meteor.users.findOne(userId, {fields: fields||{}}) : null;
}
Include this patch in your server and client code, then you can do a much more efficient operations such as:
// e.g. in a template render function:
const userName = Meteor.user({"profile.name": 1}).profile.name;
// e.g in a server method, try to fetch all the required fields in one go:
const user = Metor.user({"profile.name": 1, "emails.address": 1)};
sendEmail(user.emails[0].address, user.profile.name, ...);
This is heavily inspired by peerlibrary:user-extra
by @mitar (announced here). The difference is that my version includes a console warning to help you track down in your code unoptimized calls to Meteor.user()
, which has proven very useful to me over the years when I write new code and forget to limit the fields.
If you want to fetch the full object without a console warning, just use “Meteor.user({})
”.
There is a feature request to roll peerlibrary:user-extra
into Meteor core, but again, that doesn’t include the helpful console warning.
Give it a go - you might be surprised how many times your code is calling Meteor.user()
…