【问题标题】:Throwing Meteor.Error does not reach the clientThrowing Meteor.Error 没有到达客户端
【发布时间】:2016-12-06 01:05:35
【问题描述】:

所以基本上,客户端使用Meteor.call 调用服务器。然后,服务器方法进行一些验证并使用流星包调用 Web 服务。如果验证失败并且抛出一个流星错误,它会到达服务器。如果包响应有错误,它只会登录服务器。我需要错误信息才能联系到客户。

代码如下所示。

客户

Meteor.call('callService', (err, result) => {
    if(err) {
       console.log(err.reason);
    }
});

服务器

Meteor.methods({
    'callService'(){
        if (!Meteor.user()) {
            // Error 1
            throw new Meteor.Error('insufficient-permissions', 'You need to login first');
        }
        // Using an meteor package to actually call the service
        package.callService(apiKey, (err, response) => {
            if (response.status === 'error') {
                 // Error 2
                  throw new Meteor.Error('service-error', response.message);
            }
        });
     },
});

在服务器方法中,如果在 Error 1 处抛出错误,它确实会到达客户端,但 Error 2 不会。 错误2只登录服务器。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我猜你的 package.callService() 是异步的(因为它接受回调)。

    在这种情况下,您的 Meteor 方法会启动异步任务,然后继续其进程并返回(因为没有更多指令),而异步任务仍在运行(实际上是在等待来自远程 Web 服务的响应)。因此,您的客户端 Meteor 调用的回调会收到“无错误”响应。

    一旦发生“错误 2”,Meteor 调用已经完成,错误只能记录在服务器上。

    如果您想“挂断”您的方法,以便等待您的package.callService() 的结果来确定它是成功还是错误并相应地完成 Meteor 调用,您可以尝试使用Meteor.wrapAsync()

    顺便说一句,如果您确实使用同步任务来实际等待远程服务,您会对this.unblock() 感兴趣,以允许您的服务器处理其他任务(方法)而不仅仅是空闲。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-25
      • 2021-10-28
      • 2011-06-11
      • 1970-01-01
      • 2018-01-30
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多