1

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.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151

2 Answers2

2

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!")
   }
})
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • damn i didn't see your answer – Ethaan Feb 20 '15 at 17:53
  • "Exception in delivering result of invoking 'createNewUser': TypeError: undefined is not a function" - not giving me much that's useful with that error. – Randy Hall Feb 20 '15 at 18:19
  • Figured it out. sendEnrollmentEmail needs to be called on the server. Original answer answers original question perfectly, thanks! – Randy Hall Feb 20 '15 at 18:35
1

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');
        }
      });
    }
  });
}
Ethaan
  • 11,291
  • 5
  • 35
  • 45