Search code examples
meteormeteor-accounts

How can I add new field to Meteor.users()?


I have a button that inserts new user to Meteor.users().

In the server I have this method:

Meteor.methods({
    'addUser': function(user) {
        return Accounts.createUser(user)
    }
})

And in the client (After button is clicked):

var newUser = {
            email: t.find('#email').value,
            password: t.find('#pwd').value,
            profile: { name: t.find('#name').value, group: t.find('#userType').value },
            roles: checkedRoles // I can successfully console.log(checkedRoles) which is an array of strings.
        }

        Meteor.call('addUser', newUser, function(error){
            if(error){
                sweetAlert(error)
            } else {
                sweetAlert('User Successfully Added')
            }
        })

Using the above code, the user is added but without the roles field.

My question is, how can I add the roles field to the newly added user?


Solution

  • Use alanning:roles package:

    meteor add alanning:roles

    then (in your server side method):

    const userId = Accounts.createUser(user);

    Roles.addUsersToRoles(userId, user.roles);