Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add fields to the Meteor.users collection

Tags:

meteor

I want the Facebook accessToken that is stored in my user's document on the client. Following the meteor documentation, I should just add a new publish call.

In server.js:

Meteor.publish("access_token", function () {
  return Meteor.users().find(
    { _id : Meteor.userId() }, 
    {'services.facebook.accessToken': 1}
  );
});

In client.js:

Meteor.subscribe("access_token");

Alright, here's where I get lost. Should the accessToken just show up in the Meteor.users collection now for the logged in user? Like:

var user = Meteor.users.findOne({ _id : Meteor.userId() }); 
console.log(user); // includes services.facebook.accessToken now

Obviously, I've tried the above and the accessToken doesn't show up. Yes, I've confirmed that the mongo document contains services.facebook.

So... do I create a new client collection and somehow hook it up to the new publish? How do I get the accessToken?

like image 524
squinlan Avatar asked Oct 22 '12 20:10

squinlan


2 Answers

you should use "fields" keyword

Meteor.users.find({ _id: this.userId },
    { fields: { the-extra-fields-that-you-want-go-here: 1 } }
);

http://docs.meteor.com/#fieldspecifiers

like image 154
crapthings Avatar answered Sep 17 '22 17:09

crapthings


You can publish the field you want:

Meteor.publish( null, function() {
  Meteor.users.find({}, {fields: {profile: 1, username: 1, ...}})
}
like image 40
Tyr Avatar answered Sep 19 '22 17:09

Tyr