简体   繁体   English

投掷Meteor.Error未到达客户端

[英]Throwing Meteor.Error does not reach the client

So basically, the client calls the server using a Meteor.call . 因此,基本上,客户端使用Meteor.call调用服务器。 The server method then does some validations and calls a web service using a meteor package. 然后,服务器方法进行一些验证,并使用流星程序包调用Web服务。 If validation fails and a meteor error is thrown, it reaches the server. 如果验证失败并引发流星错误,它将到达服务器。 If the package response has an error, it only logs on the server. 如果程序包响应有错误,则仅登录服务器。 I need the error to reach the client. 我需要错误才能到达客户。

Here's how the code looks like. 代码如下所示。

Client 客户

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

Server 服务器

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);
            }
        });
     },
});

In the server method, if an error is thrown at Error 1 , it does reach the client, but Error 2 does not. 在服务器方法中,如果在错误1处引发错误 ,则错误确实会到达客户端,而错误2不会。 Error 2 only logs on the server. 错误2仅在服务器上登录。

I guess your package.callService() is async (given that it accepts a callback). 我猜你的package.callService()是异步的(假设它接受了回调)。

In that case, your Meteor method starts the async task, then continues its process and returns (since there is no more instructions), while the async task is still running (actually waiting for a response from your remote web service). 在这种情况下,您的Meteor方法将启动async任务,然后继续其过程并返回(因为没有更多指令),而async任务仍在运行(实际上正在等待远程Web服务的响应)。 Therefore your client Meteor call's callback receives a "no error" response. 因此,您的客户端Meteor呼叫的回调将收到“无错误”响应。

Once your "Error 2" happens, the Meteor call is already completed, and the error can only be logged on the server. 一旦发生“错误2”,流星调用就已经完成,并且该错误只能记录在服务器上。

If you want to "hang up" your method so that it waits for the result of your package.callService() to determine whether it is a success or an error and complete the Meteor call accordingly, you could try using Meteor.wrapAsync() . 如果要“挂断”您的方法,以便它等待package.callService()的结果以确定它是成功还是错误并相应地完成Meteor调用,则可以尝试使用Meteor.wrapAsync()

By the way, if you do use synchronous task to actually wait for a remote service, you would be interested in this.unblock() to allow your server to process other tasks (methods) instead of just idling. 顺便说一句,如果您确实使用同步任务来实际等待远程服务,您可能会对this.unblock()感兴趣,以允许您的服务器处理其他任务(方法),而不仅仅是空转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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