meteor - throw Meteor.Error总是在error.error中返回403

标签 meteor error-handling throw

由于某种原因,在我的Web应用程序中,当我在客户端回调中使用throw new Meteor.Error('my-reason', 'Full details')时,error.error的计算结果为403,而不是“my-reason”。因此,我在一个单独的文件夹中进行了快速测试:

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是“请工作”。那么,为什么在我正在开发的Web应用程序中,error.error评估为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

最佳答案

我认为问题在于,从对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 }

关于meteor - throw Meteor.Error总是在error.error中返回403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29270631/

相关文章:

javascript - 有人向我解释 {{> player}}

javascript - Meteor 和 Phaser - 加载资源时出错

meteor - 在 angle-meteor 中创建 meteor 集合的无限滚动

powershell - 命令完成后如何获取ReturnValue?

android - 通过接收 Intent 强制关闭错误

php - zend引发错误

c# - 捕获/修改(消息)/重新抛出相同类型的异常

iis - 通过 IIS ARR 代理 Meteor WebSockets

javascript - Internet连接断开时的Ajax错误处理程序

c++ - 异常如何在 C++ 中工作(在幕后)