Austin Gayler
Austin Gayler

Reputation: 4376

TypeError: Meteor.user(...).services is undefined

I'm trying to get a Meteor user's Facebook ID (who is signed in through Facebook). The following line is giving me trouble:

var userID = Meteor.user().services.facebook.id;

In the console it says

TypeError: Meteor.user(...).services is undefined

From my understanding this is because the user().services hasn't been initialized yet. I wrapped the code in if(Meteor.user()) to confirm the user has been initialized. The code is executed when a button is pressed, so it definitely has enough time to start. This started happening when I removed autopublish (although that could be unrelated). Is there anything additional I need to do to make it work without autopublish?

Upvotes: 0

Views: 637

Answers (2)

Austin Gayler
Austin Gayler

Reputation: 4376

Solved it with the following:

Meteor.publish("getUserData", function () {
    return Meteor.users.find({_id: this.userId});
});

and subscribing to that. Once that subscription is filled all of the Meteor.user.services data is available.

Upvotes: 4

Michel Floyd
Michel Floyd

Reputation: 20256

The services key is not included by default in the user's document on the client because it contains security details. You could fetch it via a method call or publish it specifically from the server and subscribe to it.

Upvotes: 0

Related Questions