Observable - 401 导致 forkJoin 出错
Observable - 401 causing forkJoin to error out
我正在使用 forkJoin 发出多个服务器请求。这是我在整个应用程序中经常使用的模式,并且效果很好。然而,我们刚刚开始实施在后端完成的用户角色。我不确定实现角色的最佳实践是什么,因为我主要是一名前端开发人员,但这是我遇到的问题:
我们的应用程序有成员和管理员成员角色。
在每个视图中,我都必须为成员和管理员成员角色调用后端,因为前端未确定角色。
成员数据始终return为成员和管理员成员这两个角色编辑,他们都有个人数据。
仅当用户是管理员时才return请求管理数据。每当用户没有管理员访问权限时,请求 return 就会出现 401 错误。这是我遇到问题的地方。
每当调用 return 出现 401 时,我的订阅方法中的错误方法就会被调用,我无权访问任何已进行的调用,包括与成员数据关联的调用。
在我包含的 forkJoin 代码中,有五个调用传递到方法中。如果用户是管理员,则第三次和第四次调用仅 return 数据,而其余调用始终为成员或管理员 returned。
当用户不是管理员时,第三次调用 returns 401 并且流停止并调用我的订阅方法中的错误处理程序。这显然不是我想要的。我希望流继续,以便我可以使用 _data 方法中的数据。
我使用 RXJS 才 6 个月,正在学习中。也许我应该使用不同的模式,或者也许有办法解决这个问题。对代码示例的任何帮助将不胜感激。在我的代码示例下方,我包含了另一个代码示例,我在其中尝试通过尝试使用 catch 方法来解决问题。没用。
我的View获取方法:
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...
}
);
}
返回 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)
.
我正在使用 forkJoin 发出多个服务器请求。这是我在整个应用程序中经常使用的模式,并且效果很好。然而,我们刚刚开始实施在后端完成的用户角色。我不确定实现角色的最佳实践是什么,因为我主要是一名前端开发人员,但这是我遇到的问题:
我们的应用程序有成员和管理员成员角色。
在每个视图中,我都必须为成员和管理员成员角色调用后端,因为前端未确定角色。
成员数据始终return为成员和管理员成员这两个角色编辑,他们都有个人数据。
仅当用户是管理员时才return请求管理数据。每当用户没有管理员访问权限时,请求 return 就会出现 401 错误。这是我遇到问题的地方。
每当调用 return 出现 401 时,我的订阅方法中的错误方法就会被调用,我无权访问任何已进行的调用,包括与成员数据关联的调用。
在我包含的 forkJoin 代码中,有五个调用传递到方法中。如果用户是管理员,则第三次和第四次调用仅 return 数据,而其余调用始终为成员或管理员 returned。
当用户不是管理员时,第三次调用 returns 401 并且流停止并调用我的订阅方法中的错误处理程序。这显然不是我想要的。我希望流继续,以便我可以使用 _data 方法中的数据。
我使用 RXJS 才 6 个月,正在学习中。也许我应该使用不同的模式,或者也许有办法解决这个问题。对代码示例的任何帮助将不胜感激。在我的代码示例下方,我包含了另一个代码示例,我在其中尝试通过尝试使用 catch 方法来解决问题。没用。
我的View获取方法:
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...
}
);
}
返回 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)
.