Search code examples
javascriptmeteortwitter-bootstrap-3meteor-accounts

How to set a profile field value based on user input in Meteor using ian:accounts-ui-bootstrap-3?


I am using the ian:accounts-ui-bootstrap-3 to create accounts in my Meteor app. In that application, I am using slightly customized version, where the user states his/her username and gender.

Accounts.ui.config({
    requestPermissions: {},
    extraSignupFields: [{
        fieldName: 'username',
        fieldLabel: 'Username',
        inputType: 'text',
        visible: true,
        validate: function(value, errorFunction) {
            if (!value) {
                errorFunction("Please write your username");
                return false;
            } else {
                return true;
            }
        }
    }, {
        fieldName: 'gender',
        showFieldLabel: false, // If true, fieldLabel will be shown before radio group
        fieldLabel: 'Gender',
        inputType: 'radio',
        radioLayout: 'vertical', // It can be 'inline' or 'vertical'
        data: [{ // Array of radio options, all properties are required
            id: 1, // id suffix of the radio element
            label: 'Male', // label for the radio element
            value: 'm' // value of the radio element, this will be saved.
        }, {
            id: 2,
            label: 'Female',
            value: 'f',
            checked: 'checked'
        }],
        visible: true
    }, ]
});

This works fine and I get a user, whose data (somewhat abridged) looks like this:

{
    "username" : "testuser",
    "emails" : [
        {
            "address" : "[email protected]",
            "verified" : false
        }
    ],
    "profile" : {
        "username" : "testuser",
        "gender" : "f"
    }
}

Now I want to use the "gender" value to set a new field called "avatar" to either "generic-male.png" or "generic-female.png". In the post https://stackoverflow.com/questions/34435674/meteor-accounts-entry-how-to-set-extrasignupfields-to-meteor-user-profile it is suggested to use the Accounts. onCreateUser mechanism. So I wrote this, and put it in the main.js file in the server catalogue:

Accounts.onCreateUser(function(user){
  if (user.gender=='m') {
    user.avatar = "generic-male.png"
  } else {
    user.avatar = "generic-female.png"
  }
})

But this produces the following error:

Exception while invoking method 'createUser' Error: insert requires an argument

I suppose this is because the user parameter is not passed to the onCreateUser method. But how should that be done?


Solution

  • You need to return user in your accountsServer.onCreateUser(func) function:

    The function should return the user document (either the one passed in or a newly-created object) with whatever modifications are desired. The returned document is inserted directly into the Meteor.users collection.

    For example:

    Accounts.onCreateUser(function(user){
      if (user.gender === 'm') user.avatar = "generic-male.png";
      else user.avatar = "generic-female.png";
      return user;
    });