Randy Hall
Randy Hall

Reputation: 8137

Accounts.createUser on server does not return response to client

I'm attempting to use Meteorjs Accounts on the server to create a new user and then send them an email to set their initial password. The idea is that an admin user can add new users.

I can successfully add the new user (I can see the new user ID in the server console if I log it), but that ID is never returned to the client. This is my server-side

Meteor.methods({
  createNewUser: function(email){
    return Accounts.createUser({email: email});
  }
});

And the relevant client-side JS:

if (isNotEmpty(email) && isEmail(email)) {
  Meteor.call("createNewUser", email, function(ret){
    if (typeof ret.message !== 'undefined') {
      if (ret.message === 'Email already exists. [403]') {
        alert("exists");
      } else {
          alert("not created");
      }
    } else {
      Accounts.sendEnrollmentEmail(ret, function(err){
        if (err){
          alert("email didn't get sent");
        } else {
          alert('success');
        }
      });
    }
  });
}

I get this in my browser console:

Exception in delivering result of invoking 'createNewUser': TypeError: Cannot read property 'message' of undefined

It's probably worth noting that I also get the "exists" alert if I try to submit the same email address twice in a row, so the error is getting returned to the client just fine.

Upvotes: 1

Views: 1046

Answers (2)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

The first argument in callback is always error object. error equals null if everything is fine.

Meteor.call('createNewUser', email, function( error, result ){
   if( error ){
      console.error("ERROR -> ", error )
   }else{
       console.log("User was created!")
   }
})

Upvotes: 2

Ethaan
Ethaan

Reputation: 11376

but that ID is never returned to the client.

Thats because you don't have any console.log on the client. also the meteor call look incorrect.

if (isNotEmpty(email) && isEmail(email)) {
  Meteor.call("createNewUser", email, function(err,result){
    if (typeof ret.message !== 'undefined') {
      if (ret.message === 'Email already exists. [403]') {
        alert("exists");
      } else {
          console.log(result) //here for example you should get the id
      }
    } else {
      Accounts.sendEnrollmentEmail(ret, function(err){
        if (err){
          alert("email didn't get sent");
        } else {
          alert('success');
        }
      });
    }
  });
}

Upvotes: 1

Related Questions