Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom fields on Meteor.users not being published

My end goal is to have CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH available to templates via {{currentUser}} if they are logged in, but Meteor is not sending down all the fields from the users collection.

In Server:

Meteor.publish('userdata', function() {
    this.ready(); // do I really even need to do this?
    //return Meteor.users.find(); //This STILL doesn't work
    return Meteor.users.findOne({_id: this.userId});
});

In Client:

var s = Meteor.subscribe('userdata', function() { 
    console.log('userdata available');
    console.log('s.ready: '+s.ready())
});
console.log('s.ready: '+s.ready())

I can verify that there are fields in the collection by connecting to the mongo instance directly and typing: db.users.find():

{
    "_id" : "N2M7Zp265vkbTBTFF",
    "createdAt" : ISODate("2013-10-15T03:29:53.155Z"),
    "CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH" : "P4GRrQMixEZducmuo",
    "profile" : {
        "name" : "Jonathan Dumaine",
            ...,
    },
    "services" : {
        "facebook" : {
            ....,
        }
    }
}

After verifying that the subscription is ready in the client, the only fields on the users collection are _id and _profile. The additional fields are not visible in the client (via console Meteor.users.find().fetch()) nor are they defined when accessed through templates.

Is this a bug? Am I doing something wrong?

like image 555
Jonathan Dumaine Avatar asked Oct 15 '13 21:10

Jonathan Dumaine


1 Answers

See this answer:

 


By default the server publishes username, emails, and profile

So you need to publish / subscribe for the additional fields.

Server:

Meteor.publish('userData', function() {
  if(!this.userId) return null;
  return Meteor.users.find(this.userId, {fields: {
    permission: 1,
  }});
});

Client:

Deps.autorun(function(){
  Meteor.subscribe('userData');
});

 


 

Looking at your code, the missing part is autorun on subscription. Without it, the subscription is called once when the app is loaded and does not change when the user changes - when he is set for the first time, for example.

like image 138
Hubert OG Avatar answered Nov 04 '22 08:11

Hubert OG