Search code examples
javascriptmeteormeteor-accounts

Server side account creation error


I have been trying to get server side account user creating to work but I have come across an issue with the check() method I am using server side. (I am using simple-schema for this)

When the password is empty, this causes check() to throw an error, and rightly so. However, this is a server-side error and I am not quite sure how to propagate this to the client to be caught and dealth with.

The exception that I can see from my browser console is as follows:

Exception while simulating the effect of invoking 'createUserAccount' Meteor.makeErrorType.errorClass {message: "Match error: One or more properties do not match the schema.", path: "", sanitizedError: Meteor.makeErrorType.errorClass, errorType: "Match.Error", stack: (...)…} Error: Match error: One or more properties do not match the schema.
at SimpleSchema.condition (http://localhost:3000/packages/aldeed_simple-schema.js?8fda161c43c0ba62801a10b0dfcc3eab75c6db88:2450:11)
at checkSubtree (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:255:17)
at check (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:67:5)
at Meteor.methods.createUserAccount (http://localhost:3000/both/methods/accounts.js?c418120e76666f0ca774a281caafc39bc2c3a59d:4:27)
at http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4244:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?81e2f06cff198adaa81b3bc09fc4f3728b7370ec:949:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4235:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4113:17)
at Object.Template.PasswordRegister.events.submit form (http://localhost:3000/client/views/shared/accounts/accounts.js?ac573d92938a2b3d6107ea19e50065f7ac5d41b3:36:20)
at null. (http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:3147:18)

Here is how my client code looks like :

Template.PasswordRegister.events({
  'submit form': function(event, template) {
    event.preventDefault();

    var user = {
      email: template.find('#email').value,
      password: template.find('#password').value
    };

    Meteor.call('createUserAccount', user, function(error) {
      if (error) {
        console.log("CONSOLE : " + error);
        //TODO DO SOMETHING
        // return alert(error.reason);
      } else {
        Meteor.loginWithPassword(user.email, user.password, function(error) {
          if (error) {
            console.log("CONSOLE : " + error);
            //TODO DO SOMETHING
            // return alert(error.reason);
          }
        });
      }
    });
  }
});

and here is my server side code:

Meteor.methods({
  createUserAccount: function(user) {
    // Important server-side check for security and data integrity
    check(user, Schema.registration);

    var email = user.email;
    var password = user.password;

    this.unblock();

    return Accounts.createUser({
      email: email,
      password: password
    });
  }
});

I've tried wrapping client-side code with a normal try catch block but didn't make any difference; that console error still shows.


Solution

  • As the error message says, you have a method stub for 'createUserAccount' defined on the client. It is that client stub that is throwing the exception.

    Wrap the method shown with if (Meteor.isServer) to keep it from running on the client.

    if (Meteor.isServer ){
      Meteor.methods({
        createUserAccount: function (user) { ... }
      });
    }
    

    If that doesn't work search your project for the client code defining the method stub.

    To clarify what is happening I have made a meteorpad with a method incorrectly stubbed on the client that throws the error you see in the browser console. I have then added a second method, 'creatUserAccount1', which is only defined on server. When this second method is called, its error is handled by callback and does not cause an exception. I think that is the behaviour you want.