Behrouz Riahi
Behrouz Riahi

Reputation: 1791

How to add extra attributes to users collection?

I am using Accounts.createUser to add new users to the database, but the problem is that, not all the attributes are added.

Here is my code to add new user:

import {Accounts} from 'meteor/accounts-base';

Template.addingUser.events({
    'submit #addUser': function (e, t) {

        e.preventDefault();

        Session.set('name', t.find('#name').value);
        Session.set('email', t.find('#email').value);
        Session.set('telephoneOffice', t.find('#telephoneOffice').value);
        Session.set('telephoneHouse', t.find('#telephoneHouse').value);
        Session.set('salary', t.find('#salary').value);

        let userId = Accounts.createUser({
            username: Session.get('name'),
            password: "123456",
            email: Session.get('email'),
            telephoneOffice: Session.get('telephoneOffice'),
            telephoneHouse: Session.get('telephoneHouse'),
            employeeSalary: Session.get('salary'),
            annualLeave: 14

        }, function (err) {
            if (err)
                console.log(err);
            else
                console.log('It worked...');
        });

        Accounts.sendEnrollmentEmail(userId);


    }
});

only the name, email, and password are added.

How do I include the other information as well such as telephoneOffice?

Upvotes: 0

Views: 203

Answers (2)

lnmunhoz
lnmunhoz

Reputation: 115

You need to pass the extra data inside the profile object.

Accounts.createUser({
  username: Session.get('name'),
  password: "123456",
  email: Session.get('email'),
  profile: {
    telephoneOffice: Session.get('telephoneOffice'),
    telephoneHouse: Session.get('telephoneHouse'),
    employeeSalary: Session.get('salary'),
    annualLeave: 14
  }
  ...

Upvotes: 2

kooc
kooc

Reputation: 146

Accounts.createUser does not accept custom arguments beyond username, email, password, and profile. The default functionality for passing custom user info is to pass those fields such as telephoneOffice as part of the profile object, which is copied to user.profile in the document inserted to the user collection.

For example:

let userId = Accounts.createUser({
        username: Session.get('name'),
        password: "123456",
        email: Session.get('email'),
        profile: {
          telephoneOffice: Session.get('telephoneOffice'),
          telephoneHouse: Session.get('telephoneHouse'),
          employeeSalary: Session.get('salary'),
          annualLeave: 14
        }
    });

Note that the user.profile fields are by default modifiable by users. So it's there by legacy, but Meteor actually recommends avoiding using it for storage.

If you want those fields to be on user instead of user.profile, What you can do is to pass your custom params on the profile object as above, and then override the default behavior using Accounts.onCreateUser. Something like this:

Accounts.onCreateUser(function(options, user) {
  if (options.profile)
    _.extend(user, options.profile);
  return user;
});

See more info here: https://guide.meteor.com/accounts.html#custom-user-data

Upvotes: 1

Related Questions