I am refactoring my publications and am running into a lot of trouble with 2 publications:
// Publication that returns the public profile of all users
// Used in a global iron router controller
Meteor.publish("publicProfile", function() {
return Meteor.users.find(
{},
{
fields: {
'createdAt':1,
'profile.name': 1,
'profile.lastName': 1,
'profile.avatarUrl': 1,
'profile.language': 1,
'data.team':1,
'roles': 1,
"emails":1,
'status':1,
}
});
});
// Publication that returns all info about a single user for the admins
Meteor.publish("singleUser", function(userId) {
return Meteor.users.find({_id:userId});
});
// OnCreated call in the admin panel
Template.admin_long_profile.onCreated(function(){
var self = this;
this.autorun(function(){
var userId = Session.get('currentProfile');
var handler = self.subscribe("singleUser",userId);
// Get the current user
if (handler.ready()) {
self.user = Meteor.users.findOne({_id:userId});
console.log(self.user);
}
});
});
Now, the problem is that even though I successfully subscribe to âsingleUserâ (according to Kadira debug) the user object only contains the fields from the publicProfile subscription. Kadira debug even tells me that one document in my user collection changed. What could be the reason for this? It seems like a standard pattern and I havenât ever had problems with subscribing to the same collection multiple times.
The trouble here is about it being the Meteor.users collection, not because of multiple subscriptions to the same collection, those are indeed fine.
From my experience, though not necessarily from reading the Meteor docs, I gathered that you always have to define which fields you want to publish for the Meteor.users collection. It seems that this one is, and probably sensibly so, restricted by default. Otherwise itâd be really easy to mess up and accidentally publish all user passwords.
So just always specify the fields you want published for that particular collection and itâll work just fine. Canât spot anything else off in your code.
And you are absolutely sure that nothing else is going wrong, i.e. wrong or no userId, the .ready() never firing, the record not being in the DB etc?
Also, the msavin:mongol package is often helpful for looking at whatâs going on with subscriptions, i.e. whatâs currently on the client.
I have added console.log inside the publication and it prints out the right id. I have also printed out the âuserâ object from inside the handle.ready() call and it contained the correct user object without the additional fields. Everything is working sans the publication of the additional fields for the single userâŚ
Well, that only tells you that the other publication is working, but potentially nothing about the one youâre trying to debug. Disable the other one, see what happens.
Everything looks fine with your code. Meteor does not restrict the fields published from the user collection in any way and everything you are doing should work fine. If you call Meteor.users.find() and return it from a publish function, it will publish ever user with every field. Multiple publications of the same documents also should not affect each other at the top level keys, as the merge box will take care of this. Second level keys on the document will be completely overwritten, but that doesnât affect you in this case.
The only thing I can think of is maybe look into another package that might be affecting this.
So, things are starting to get weird. I commented out the âpublicProfileâ subscription and used the console to manually subscribe to the âsingleUserâ publication. And it worked -.- Somehow the publicProfile publication overrules the single user publicationâŚ
EDIT: actually, what I have done is I navigated to the Template and called Session.set() in the console. Everything worked fine, so there is no issue with the autorun function or the publication/subscription itself. The problem only occurs when publicProfile and singleUser are used in conjunction