Can't analyze error object (return from method call)! Totally stumped

So in a method call, I’ve got this code:

    if newUserId # successfully made new user
      Accounts.sendVerificationEmail newUserId
      return { success: true }
    else
      throw new Meteor.Error 'user-exists', 'User already exists.'

And back in the script that calls the method:

        Meteor.call 'createSeller', newUser, (error, result) ->
          Session.set 'creatingUser', false
          console.dir error

I would expect it to dump out the error object and show its properties and values, but instead it just dumps out the following:

Error: Username already exists. [403]
Stack trace:
Meteor.makeErrorType/errorClass@http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:532:15
._livedata_result@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4964:23
Connection/onMessage@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:3725:7
._launchConnection/self.socket.onmessage/<@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2717:11
_.forEach@http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:7
._launchConnection/self.socket.onmessage@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2716:9
SockJS</REventTarget.prototype.dispatchEvent@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:156:9
SockJS</SockJS.prototype._dispatchMessage@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1141:5
SockJS</SockJS.prototype._didMessage@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1199:13
SockJS</SockJS.websocket/that.ws.onmessage@http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1346:9

I don’t get it. Why is it dumping all this out? typeof error returns “object.” Then why do console.dir and console.log not show it as it shows most objects? e.g.:

Object { name: "Bob", age: 48 }

This error might be coming from sendVerificationEmail call. try reseting your app with meteor reset on the shell. and try again.

sendVerificationEmail might not send an email if the user already exists.

Hmm, now I get this:

"Exception in delivering result of invoking ‘createSeller’: [“submit form”]/</<@http://localhost:3000/client/templates/seller/seller-create.coffee.js?224060aa0e4015a96a402b5ad1055c1d5364bcb7:21:1

That error doesn’t tell me much, unfortunately. Could use a little more detail.

you must be doing something else in the method before if(newUserId) that is causing that exception

Full method:

  createSeller: (userData) ->
    check userData,
      username: String
      email: String
      password: String
      profile: Match.Optional Match.ObjectIncluding({accountType: String})

    newUserId = Accounts.createUser
      username: userData.username
      email: userData.email
      password: userData.password
      profile:
        accountType: 'seller'

    if newUserId # successfully made new user
      Accounts.sendVerificationEmail newUserId
      return { success: true }
    else
      throw new Meteor.Error 'user-exists', 'User already exists.'

Okay @Murwade, I figured out where the problem is but don’t know why. Any error I throw after the Accounts.createUser call returns a 403, no matter what error I want to throw.

That’s because you already have an error thown from within Accunts.createUser before your if control runs.

What you should do is:

  • if you are calling Accounds.createUser from the client, use its callback to get the error object
  • if you are on the server, then use either Accounts.validateNewUser or Accounts.onCreateUser the latter which provides a good set of tools

These options are good for checking/manipulating the user object before its creation.

To do further tasks after the creation, you can use one of:

Or craft it manually using one of these patterns:

This one taken from https://github.com/awatson1978/meteor-cookbook/blob/master/cookbook/observers.md#accountsonaftercreateuser

Meteor.users.find({ initialized: { $exists: false } }, { fields: { _id: 1 } }).observe({
  added: function (user) {
    // insert default user docs here if not already present
   Meteor.users.update(user._id, { $set: {initialized: true} });
  }
});

or this one using the undocumented Meteor.users._makeNewID() call

Accounts.onCreateUser(function(options,user){
  // Hack to create user id so that we can initialize is counts.
  user._id = Meteor.users._makeNewID();
  // do something with this userId
  return user;
});