由于某种原因,在我的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/