抛出Meteor.Error总是在error.error中返回403

时间:2015-03-26 03:32:30

标签: meteor error-handling throw

出于某种原因,在我的网络应用中{I} throw new Meteor.Error('my-reason', 'Full details')时,在客户端回调中,error.error评估为403而不是“我的理由”。所以我在一个单独的文件夹中进行了快速测试:

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

果然,它很有效,error.error是“工作愉快”。那么,为什么,在我正在开发的网络应用程序中,error.error eval到403?来自所述网络应用的相关摘要:

服务器:

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

客户端:

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

1 个答案:

答案 0 :(得分:0)

我认为问题是从Accounts.createUser的调用中抛出错误,而不是返回 错误,而不是您手动创建的错误,是什么被送回客户端。换句话说,你永远不会达到

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

因为在你到达那一点之前执行已经停止。

我认为您可以通过从Accounts.createUser调用中删除错误来解决此问题:

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 }