[SOLVED ]Can't create new users: Accounts.createUser() --> undefined

EDIT: I solved this. See my response below.

No matter what I try, I can’t create new users. Even when I enter: Accounts.createUser({username: "user", email: "me@email.com", password: "pass"}); in the console, I just get undefined with no actual error message. I’ve searched everywhere and can’t figure this out. When the below code runs, console.log prints out newUserData with no problems, but no user is added.

Template.signup.events({
        'submit form': function(event) {
            event.preventDefault();
            var newUserData = {
              username: event.target.username.value,
              email: event.target.email.value,
              password: event.target.password.value
            };
            Accounts.createUser(newUserData);
            console.log(newUserData);
        }
    });

@methodx,

What does the db show? I notice you are not checking for the user before the create. Also, i take it you have insecure and autopublish turned on? Put a try catch around the Accounts.createUser and see what error comes back I guess?

As an example, this is what I have in my UI:

const user = [{
      email: userEmail,
      password: userPassword,
      profile: {
        name: { first: userFirstName, last: userLastName },
      },
      roles: userRoles,
    }];

    Meteor.call('users.insert', user);

And the users.insert function on the server side looks like this…

Meteor.methods({
  'users.insert'(users) {
    check(users, [{
      email: String,
      password: String,
      profile: { name: { first: String, last: String } },
      roles: [String],
    }]);

    users.forEach(({ email, password, profile, roles }) => {
      const userExists = Accounts.findUserByEmail(email);

      if (!userExists) {
        const userId = Accounts.createUser({ email, password, profile });
        Roles.addUsersToRoles(userId, roles, <my whatever global role group>);
      }
    });
  },
...

Thank you very much for the reply and example code. The error I got:

Error: options.password must be a string

I’m using a standard input box.

     <div class="ui fluid left icon input field">
        <i class="user icon"></i>
        <input id="username" type="text" placeholder="Enter a username...">
      </div>
      <div class="ui fluid left icon input field">
        <i class="mail icon"></i>
        <input id="email" type="email" placeholder="Enter your email address...">
      </div>
      <div class="ui fluid left icon input field">
        <i class="lock icon"></i>
        <input id="password" type="password" placeholder="Enter a unique password...">
      </div>

I solved this issue. For some reason, even though I have insecure and autopublish on, I had to still create the user on the server.

On the client:

// Signup Form Submit
Template.signup.events({
    'submit form': function(event) {
        event.preventDefault();
        var newUserData = {
          username: $('[id="username"]').val(),
          email: $('[id="email"]').val(),
          password: $('[id="password"]').val()
        };
        Meteor.call('insertUser', newUserData);
        Meteor.loginWithPassword(newUserData.email, newUserData.password);
    }
});

On the server:

// Create a new user
Meteor.methods({
  insertUser:function(newUserData){
    return Accounts.createUser(newUserData);
  }
});
2 Likes