简体   繁体   English

可观察-401导致forkJoin错误输出

[英]Observable - 401 causing forkJoin to error out

I am using forkJoin to make several server requests. 我正在使用forkJoin发出多个服务器请求。 This is a pattern I have commonly been using through out my application and it has been working great. 这是我在整个应用程序中一直使用的一种模式,并且运行良好。 However we just started implementing user roles which is done on the backend. 但是,我们只是开始实现在后端完成的用户角色。 I am not sure what is the best practice for implementing roles as I am mostly a front end developer, nonetheless this is the problem I have encountered: 我不确定实现角色的最佳实践是什么,因为我主要是前端开发人员,但这是我遇到的问题:

Our application has member and admin member roles. 我们的应用程序具有成员和管理员成员角色。

  1. From each view I must make calls to the backend for both member and admin member roles regardless as roles are not determined on the frontend. 从每种观点来看,无论角色是在前端确定的,我都必须同时调用成员角色和管理员成员角色的后端。

  2. Member data is always returned in for both roles as members and admin members both have personal data. 成员和管理员成员都具有个人数据时,总是同时返回两个角色的成员数据。

  3. Requests made for admin data is only returned when the user is an admin. 仅当用户是管理员时,才返回对管理员数据的请求。 Whenever the user does not have admin access the request returns a 401 error. 每当用户没有管理员访问权限时,请求都会返回401错误。 This is where I am having a problem. 这是我遇到的问题。

Whenever the call returns a 401, the error method in my subscribe method is invoked and I do not have access to any of the calls that were made including the calls associated to the member data. 每当调用返回401时,都会调用我的subscription方法中的error方法,并且我无权访问所进行的任何调用,包括与成员数据关联的调用。

In my included code within the forkJoin there are five calls passed into the method. 在我在forkJoin内包含的代码中,有五个调用传递给该方法。 The third and forth call only return data if the user is an admin while the rest of the calls are always returned for either member or admin. 如果用户是管理员,则第三次和第四次呼叫仅返回数据,而其余的呼叫总是针对成员或管理员返回。

When the user is not an admin the third call returns a 401 and the stream stops and the error handler in my subscribe method is invoked. 当用户不是管理员时,第三个调用返回401,并且流停止并且我的subscription方法中的错误处理程序被调用。 This is obviously not what I want. 这显然不是我想要的。 I want the stream to continue so I can use the data in the _data method. 我希望流继续进行,以便可以在_data方法中使用数据。

I have only been using RXJS for 6 months and am learning. 我只使用RXJS 6个月了,正在学习。 Maybe I should be using a different pattern or maybe there is a way to fix this. 也许我应该使用其他模式,或者有办法解决这个问题。 Any help with code examples would be greatly appreciated. 与代码示例的任何帮助将不胜感激。 Below my code example I included another example of code in which I attempted to fix the problem by playing around with catch methods. 在我的代码示例下面,我包括另一个代码示例,其中我尝试通过使用catch方法来解决问题。 It didn't work. 没用

My View get method: 我的视图获取方法:

private getZone() {
  this.spinner.show();
  this.zonesService.getZone(this.zoneId)
    .map(response => {
      this.zone = response['group'];
      return this.zone;
    })
    .flatMap(() => {
      return Observable.forkJoin(
        this.teamsService.getTeam(this.zone['TeamId']),
        this.zonesService.getZoneAssociations(this.zone['id'], '/myDevices'),
        this.zonesService.getZoneAssociations(this.zone['id'], '/devices'),
        this.zonesService.getZoneAssociations(this.zone['id'], '/groupMembers'),
        this.sitesService.getSite(this.zone['SiteId'])
      );
    })
    .subscribe(
      _data => {
        // data handling...
      },
      _error => {
        // error handling ...
      }
    );
}

My attempt to fix: 我尝试修复:

private getZone() {
  this.spinner.show();
  this.zonesService.getZone(this.zoneId)
    .map(response => {
      this.zone = response['group'];
      return this.zone;
    })
    .flatMap(() => {
      return Observable.forkJoin(
        this.teamsService.getTeam(this.zone['TeamId']),
        this.zonesService.getZoneAssociations(this.zone['id'], '/myDevices'),
        this.zonesService.getZoneAssociations(this.zone['id'], '/devices')
          .catch(error => Observable.throw(error)),
        this.zonesService.getZoneAssociations(this.zone['id'], '/groupMembers')
          .catch(error => Observable.throw(error)),
        this.sitesService.getSite(this.zone['SiteId'])
      );
    })
    .subscribe(
      _data => {
        // data handling...
      },
      _error => {
        // error handling...
      }
    );
}

Returning Observable.throw will just rethrow the caught error, which will see forkJoin emit the error. 返回Observable.throw只会重新抛出捕获的错误,这将看到forkJoin发出错误。

Instead, you could use Observable.of(null) to emit null and then complete, which will see forkJoin emit a null for the observable that emitted the error: 相反,您可以使用Observable.of(null)发出null ,然后完成,这将看到forkJoin为发出错误的observable发出null

  return Observable.forkJoin(
    this.teamsService.getTeam(this.zone['TeamId']),
    this.zonesService.getZoneAssociations(this.zone['id'], '/myDevices'),
    this.zonesService.getZoneAssociations(this.zone['id'], '/devices')
      .catch(error => Observable.of(null)),
    this.zonesService.getZoneAssociations(this.zone['id'], '/groupMembers')
      .catch(error => Observable.of(null)),
    this.sitesService.getSite(this.zone['SiteId'])
  );

Or, if you wanted to emit the error as a value, you could use Observable.of(error) . 或者,如果您想将错误作为值发出,则可以使用Observable.of(error)

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

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