1
votes

I have the following code in my Meteor app where I create new users, assign them 'basic' role. Yet I am having a trouble showing on the client side errors returned while processing Accounts.createUser, can someone please tell me how I can return errors returned by Accounts.createUser while having it on the server as my code below. Thanks

/server/users.js

          Meteor.methods({
            'createMemberAccount': function (data, role) {    
              var userId;

              Meteor.call('createNewAccount', data, function(err, result) {
                  if (err) {
                    return err;
                  }
                  console.log('New account id: '+ result);

                  Roles.addUsersToRoles(result, role);
                  return userId = result;
              });

              return userId;
            },
            'createNewAccount': function (adminData) {
                return Accounts.createUser({email: adminData.email, password : adminData.password, roles: adminData.roles});
            }
          });

/client/signup.js

        Template.signupForm.events({

          'submit #signup-form': function(e, t){
            e.preventDefault();

            var userData = {};
            userData.email = $(e.target).find('[name=email]').val();
            userData.password = $(e.target).find('[name=password]').val();
            userData.roles = ['basic'];

              Meteor.call('createMemberAccount', userData, 'basic', function(err, userId) {
                  if (!err) {
                    console.log('All OK');
                  } else {
                    console.log('Error: ' + err.message);
                  }
                });
            return false;
          }

        });
3

3 Answers

1
votes

Since You are creating an static rol "basic", you don't need to do that pair of methods, and Meteor.calls, instead you can use

So, use the v on the client side, just like this.

Template.register.events({
    'submit #register-form' : function(e, t) {
      e.preventDefault();
      var email = t.find('#account-email').value
        , password = t.find('#account-password').value;

        // Trim and validate the input

      Accounts.createUser({email: email, password : password}, function(err){
          if (err) {
            // Inform the user that account creation failed
          } else {
            // Success. Account has been created and the user
            // has logged in successfully. 
          }

        });

      return false;
    }
  });

If you see there is not any role yet incude, so now on the server.js use the onCreateUser method.

//Server.js
Accounts.onCreateUser(function(options, user) {
  if (options.profile)
    user.profile = options.profile;
    user.role = "basic"
  return user;
});

Now thats is more easy, and with less code, if you are trying to create 2 differents roles like "Admin" and "Basic", just on the client side create a profile field named "profile.roles" and do a if statement on the onCreateUser.

0
votes
return Accounts.createUser({email: adminData.email, password : adminData.password, roles: adminData.roles});

This part returns the userId once it is created, it doesn't return any errors when it fails.

When it fails, the returned value will be undefined

Also, in the server, we cannot use callbacks with Accounts.createUser If you want find the errors, you have to use Accounts.createUser in client side.

0
votes

Coming to this late, but on the server side, you can assign the createUser to a variable and it will return the new user’s _id; then you can check if that exists. For example (server side only):

let email = '[email protected]';
let password = 'bar';
let profile = {firstName: 'foo', lastName: 'bar'};
let newId = Accounts.createUser({
    password: password,
    email: email,
    profile: profile
});
if (!newId) {
    // New _id did not get created, reason is likely EMail Already Exists
    throw new Meteor.Error(403, "Cannot create user: " + error.reason);
}
else {
    // Stuff here to do after creating the user
}

The Meteor.Error line will be passed back as an error in the callback on the client side, so you can reflect that error to the browser.