Having trouble adding custom fields to user


#1

So I’m attempting to use:

Accounts.onCreateUser(function(user) {
  user.sub = {};
  return user;
});

To add a custom field to the user object - but the above code seems to intefer with:

Accounts.createUser({
  email: email,
  password: password,
  profile: profile
}, […]

Doing the above means that I capture all of the data as expected, and create the extra field, but no email address is saved. Seems weird but there you go. Profile and password are saved fine, but email is ignored.

If I remove the Accounts.onCreateUser code then the email is saved just fine.

The docs state clearly that either a username or email is required (for obvious reasons), so I’m not even sure how the accounts system is able to create the user without a username or email address…

So what gives? Is this a bug?


#2

Ah, cracked it, the clue was in the docs example actually, I needed:

Accounts.onCreateUser(function(options, user) {
  user.sub = {};
  if (options.profile) {
    user.profile = options.profile;
  }
  if (options.email) {
    user.email = options.email;
  }
  return user;
});

The profile check doesn’t actually seem to be needed, but as it’s in the docs and I’ve seen the issue with the email address I figure it’s safer to include it.