Ad

Meteor - Cannot Access Custom User Field Upon Registration

- 1 answer

Upon a user registration, I've added a new field for a Meteor user using React as my front end:

React component:

Accounts.createUser({
 email: trimInput(this.state.email),
 password: this.state.password,
 username: trimInput(this.state.username),
 regId: 1, // I need to access this to show/hide fields elsewhere.
});

Server:

// I assume this is how it's done?
Accounts.onCreateUser(function(options, user) {
  user['regId'] = options.regId
  return user
});

In meteor mongo I can see the newly added field: "regId": "1". Great. Now in browswer console: Meteor.user.find().fetch() does not include the field. Meteor.user().regId returns undefined. Huh?

What is the correct way to approach something like this?

Ad

Answer

I think previous answer is good, but you should improve in order to remove Meteor.subscribe("userData"); in every controller by publish automatically.

`Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'regId': 1}});
  } else {
    this.ready();
  }
});`
Ad
source: stackoverflow.com
Ad