0
votes

I have a Meteor application and am trying to register a user and assign a role:

In the client registration file, I have a user with email, password, username, profile (firstname and lastname) and role:

 Accounts.createUser(user, user.roles, function(error){
   if(!error) {
     FlowRouter.go('home');
     console.log("Registration successful");
   }
   else {
     FlashMessages.sendError(error.reason);
     console.log("Registration not successful: " + error.reason);
   }
 });

And then in the server file I have:

Accounts.onCreateUser(function(options, user) {
    if(options.roles) {
        user.roles = options.roles;
        Roles.addUsersToRoles(user._id, user.roles);
    }
    return user;
});

With this code, I always get an error message:

Exception in delivering result of invoking 'createUser': TypeError: options.userCallback.apply is not a function
    at http://localhost:3000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:612:26
    at http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:794:19
    at loggedInAndDataReadyCallback (http://localhost:3000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:708:7)
    at null._callback (http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22)

The user does get added successfully though!

EDIT: THE FIX

On server side:

Accounts.onCreateUser(function(options, user) {
    if (options.profile) {
        user.profile = options.profile;
    }
    if(options.roles) {
        user.roles = options.roles;
        Roles.addUsersToRoles(user._id, user.roles);
    }
    return user;
});

On client side:

Accounts.createUser(user, function(error){
                if(!error) {
                    FlowRouter.go('home');
                }
                else {
                    FlashMessages.sendError(error.reason);
                }
            });
1

1 Answers

1
votes

You are adding the roles to the user in onCreateUser. The user object has not been inserted in the collection at that point. Another remark: you pass user.roles as your second parameter to createUser. According to Meteor docs you can only pass options and a callback. See http://docs.meteor.com/#/full/accounts_createuser

Maybe change to something like:

Accounts.createUser(user, function(error){
  if(!error) {
    Roles.addUsersToRoles(user._id, user.roles);
    FlowRouter.go('home');
    console.log("Registration successful");
  } else {
    FlashMessages.sendError(error.reason);
    console.log("Registration not successful: " + error.reason);
  }
});