简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM