msavin / userCache

The one simple trick that can save you millions of database requests
MIT License
35 stars 6 forks source link

returned user doc doesn't always include _id field when from cache #9

Open wildhart opened 5 years ago

wildhart commented 5 years ago

I've had instances where the returned user doc doesn't include the _id field.

This can cause a bug in code like this:

// toggle the 'flag' of user given by 'username', or the logged in user
const user = username
   ? Meteor.users.findOne({username}, {fields: {[flag]: 1}})
   : Meteor.user(flag);
Meteor.users.update({id: user._id}, {$set: {[flag]: !user[flag]}});

Which fails if username is falsey and the cached Meteor.user() doesn't contain the _id field.

Obviously this is a contrived example which could easily be rewritten, but I did hit this bug and so userCache should be fixed to return the expected result consistent with the default Meteor.user().

I have a PR to fix this nearly ready...