简体   繁体   中英

Throwing Meteor.Error always returns 403 in error.error

For some reason, in my web app when I throw new Meteor.Error('my-reason', 'Full details') , in the client callback, error.error evaluates to 403 instead of "my-reason." So I did a quick test in a separate folder:

if Meteor.isClient
  Template.stuff.events
    'click .throw': ->
      Meteor.call 'throwError', 'dummy data', (error, result) ->
        console.log error.error

if Meteor.isServer
  Meteor.methods
    throwError: (str) ->
      throw new Meteor.Error 'work-please', 'Please work.'

Sure enough, it works great, and error.error is "work-please." So why, in my web app I'm developing, does error.error eval to 403?? Relevant snippets from said web app:

Server:

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.'

Client:

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

I think the issue is that the error is thrown, not returned, from the call to Accounts.createUser , and that error, not the error you're manually creating, is what's being sent back to the client. In other words, you're never even reaching

else
    throw new Meteor.Error 'user-exists', 'User already exists.'

because execution has stopped before you reach that point.

I think you can work around this by catching the error out of the Accounts.createUser call:

try { 
newUserId = Accounts.createUser
    username: userData.username
    email: userData.email
    password: userData.password
    profile:
      accountType: 'seller'
} catch (err) { 
  throw new Meteor.Error 'user-exists', 'User already exists.'
}

// If you get to here you know there wasn't an error so no need for if statement
Accounts.sendVerificationEmail newUserId
return { success: true }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM