11943

Getting Facebook Avatar in Meteor when Autopublish is removed

Question:

Currently when auto publish is removed, only {{currentUser.profile.name}} works.I'm trying to get {{currentUser.profile.first_name}} and the avatar from Facebook but have not been able to do so. Here is my code...

On the Server side:

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

On Iron Router:

Router.configure({ waitOn: function() { return Meteor.subscribe('userData'); } });

From my understanding, I see that Meteor is publishing all the userData and then subscribing to it via Iron Router. What I don't understand is why this is not working -- as I think {{currentUser.profile.first_name}} should work but isn't.

Answer1:

Like Richard suggests, when a user is created, you can copy the services document to the profile doc.

Accounts.onCreateUser(function(options, user) { // We still want the default hook's 'profile' behavior. if (options.profile) { user.profile = options.profile; user.profile.memberSince = new Date(); // Copy data from Facebook to user object user.profile.facebookId = user.services.facebook.id; user.profile.firstName = user.services.facebook.first_name; user.profile.email = user.services.facebook.email; user.profile.link = user.services.facebook.link; } return user; });

Your publication to get their first name and Facebook ID would look like this...

/* ============== Single User Data =============== */ Meteor.publish('singleUser', function(id) { check(id, String); return Meteor.users.find(id, {fields: {'profile.facebookId': 1, 'profile.name': 1, 'profile.firstName': 1, 'profile.link': 1}}); });

You can access a user's Facebook avatar with a template helper function...

Template.profileView.helpers({ userPicHelper: function() { if (this.profile) { var id = this.profile.facebookId; var img = 'http://graph.facebook.com/' + id + '/picture?type=square&height=160&width=160'; return img; } } });

In your template, you can then use the following helper (provided you are wrapping this in a block that contains user data):

<img src="{{userPicHelper}}" alt="" />

Answer2:

I believe you're trying to access the first_name field from the services subdocument. It should be {{currentUser.services.facebook.first_name}}

If you want to transfer first_name to the profile subdocument, you can have the following event handler:

Accounts.onCreateUser(function(options, user) { // ... some checks here to detect Facebook login user.profile.firstName = user.services.facebook.first_name; user.profile.lastName = user.services.facebook.last_name; });

Recommend

  • Join in Meteor with publishComposite
  • Receiving a broadcast message in C# [duplicate]
  • How to insert object into meteor collection
  • Correct way to manage routes after sign in for different roles
  • Loading multiple Flot pie charts from external js file as needed
  • Issue with accessing subscriptions in Meteor
  • How do I send a command to a Mac via WiFi from an iPhone?
  • Web API Basic Auth inside an MVC app with Identity Auth
  • Creating a Messenger service
  • SiteMesh: Changing the content-type of the response
  • Disadvantages to high make job values
  • NServiceBus - NServiceBus.Host as publisher and WPF app as subscriber. How To?
  • Ionic2: Unsubscribe Event to Avoid Duplicate Entries?
  • const char **a = {“string1”,“string2”} and pointer arithametic
  • SIP Makefile fail (gnuwin and mingw)
  • How to generate an asynchronous reset verilog always blocks with chisel
  • Search function not doing anything
  • How can I let users share their location in Bot Framework webchat channel?
  • SIP API media codecs
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How do I configure context broker accept post requests from my remote sensor?
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Using jQuery closest() method with class selector
  • Array.prototype.includes - not transformed with babel
  • swift auto completion not working in Xcode6-Beta
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Matrix multiplication with MKL
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Converting MP3 duration time
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to load view controller without button in storyboard?