Edgar
Edgar

Reputation: 931

Meteor.Error - custom [reason]

I am throwing a Meteor.Error with a custom [reason] like this:

if (Meteor.isServer) {

  Accounts.onCreateUser(function(options, user) {
    ...
    if (emailAlreadyExist === true) {
      throw new Meteor.Error(403, "email already registered");
    } else {
      return user
    }
  })
}

then trying to show this error message to the user:

if (Meteor.isClient) {
...
    Accounts.createUser({
      email: email,
      password: password
    }, function(error) {
      if (error) {
        // Inform the user that account creation failed
        sAlert.error(error.reason);
      } else {
        // Success.
      }
    });
...
}

But this alert does not show my custom Meteor.Error [reason], it always gives me "Email already exists". What I am doing wrong?

Upvotes: 2

Views: 154

Answers (1)

Stephen Woods
Stephen Woods

Reputation: 4049

I think it's because Accounts.createUser() is throwing that error, which happens before your onCreateUser and thus the callback executes.

Upvotes: 2

Related Questions