【问题标题】:Observable - 401 causing forkJoin to error outObservable - 401 导致 forkJoin 出错
【发布时间】:2017-12-18 14:30:30
【问题描述】:

我正在使用 forkJoin 发出多个服务器请求。这是我在整个应用程序中经常使用的模式,并且效果很好。然而,我们刚刚开始实现在后端完成的用户角色。我不确定实现角色的最佳实践是什么,因为我主要是前端开发人员,但这是我遇到的问题:

我们的应用程序具有成员和管理员成员角色。

  1. 从每个视图中,我必须为成员和管理员成员角色调用后端,无论角色在前端没有确定。

  2. 两个角色的成员数据始终返回,因为成员和管理员成员都有个人数据。

  3. 仅当用户是管理员时才返回对管理员数据的请求。只要用户没有管理员权限,请求就会返回 401 错误。这就是我遇到问题的地方。

只要调用返回 401,就会调用我的订阅方法中的错误方法,并且我无权访问已进行的任何调用,包括与成员数据关联的调用。

在我包含在 forkJoin 中的代码中,有五个调用传递到该方法中。第三次和第四次调用仅在用户是管理员时返回数据,而其余调用始终为成员或管理员返回。

当用户不是管理员时,第三次调用返回 401 并且流停止并且我的订阅方法中的错误处理程序被调用。这显然不是我想要的。我希望流继续,以便我可以使用 _data 方法中的数据。

我只使用 RXJS 6 个月,并且正在学习。也许我应该使用不同的模式,或者有办法解决这个问题。任何有关代码示例的帮助将不胜感激。在我的代码示例下面,我包含了另一个代码示例,我在其中尝试通过使用 catch 方法来解决问题。没用。

我的视图获取方法:

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

我的修复尝试:

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

【问题讨论】:

    标签: javascript angular rxjs observable fork-join


    【解决方案1】:

    返回Observable.throw 只会重新抛出捕获的错误,这将看到forkJoin 发出错误。

    相反,您可以使用Observable.of(null) 发出null 然后完成,这将看到forkJoin 为发出错误的可观察对象发出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'])
      );
    

    或者,如果您想将错误作为值发出,您可以使用Observable.of(error)

    【讨论】:

    • 谢谢。这太棒了,而且效果很好。
    • 这是一个很好的答案,你应该得到更多的积分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2018-10-02
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多