Alex Beauchemin
Alex Beauchemin

Reputation: 1181

Meteor user profile always undefined

I can't see any profile information on my users, any idea why?

server :

Meteor.publish("userData", function () {
    return Meteor.users.find({_id: this.userId},
        {fields: {'profile': 1}});
});
Meteor.publish("allUsers", function () {
    //TODO: For testing only, remove this
    return Meteor.users.find({}, {fields: {'profile': 1}});
});

client :

Meteor.autosubscribe(function () {
    Meteor.subscribe('allUsers',null , function() { console.log(Meteor.users.find().fetch()) });
    Meteor.subscribe('userData', null, function() { console.log(Meteor.user())});
});

....

Accounts.createUser({email:email,password:password, profile: {name: name}},function(error){
    ...
});

My console output an object with only _id and emails for the first one and undefined for the second one. The profile information (name in my case) seems to works because in my server.js I have a name validation that works fine :

Accounts.onCreateUser(function(options, user) {
    if(options.profile.name.length<2)
        throw new Meteor.Error(403, "Please provide a name.");
    return user;
});

Am I missing something?

Thanks!

Upvotes: 2

Views: 3306

Answers (3)

JAMESSTONEco
JAMESSTONEco

Reputation: 2051

Here is the workaround I am using, placed in ~/server/createAccount.js The issue I was having is that I would get errors where profile was undefined. This seems to fix things by creating the profile when the account is created.

Hope this is useful. Found it on a github issue comment, in the comment below:

// BUGFIX via https://github.com/meteor/meteor/issues/1369 @thedavidmeister
// Adds profile on account creation to prevent errors from profile undefined on the profile page
Accounts.onCreateUser(function(options, user) {
  user.profile = options.profile ? options.profile : {};
  return user;
});

Upvotes: 1

Alex Beauchemin
Alex Beauchemin

Reputation: 1181

Found the problem :

In the onCreateUser function , I need to add this the profile information from the options to the user object , so my function should looks like this instead :

Accounts.onCreateUser(function(options, user) {
    if(options.profile.name.length<2)
        throw new Meteor.Error(403, "Please provide a name.");
    if (options.profile)
    user.profile = options.profile;
    return user;
});

Upvotes: 3

Tarang
Tarang

Reputation: 75945

When using multiple subscriptions, only the first subscription matters. The second subscription, if containing the same collection is ignored because it conflicts with the first.

You could do this instead, though:

server:

var debugmode = false; //set to true to enable debug/testing mode
Meteor.publish("userData", function () {
    if(debugmode) {
        return Meteor.users.find({}, fields: {'profile': 1}});
    }
    else
    {
        return Meteor.users.find({_id: this.userId},{fields: {'profile': 1}});
    }
});

client:

Meteor.autosubscribe(function () {
    Meteor.subscribe('userData', null, function() { console.log(Meteor.user()); console.log(Meteor.users.find({}).fetch());});
});

Upvotes: 3

Related Questions