我竭力要实现打字稿& RxJs一个函数,返回观察数据的从后端。我需要根据用户角色查询不同的端点。我正在使用angular-JWT进行身份验证。我有两个问题:查询不同的端点
目前代码编译很好,但第二对请求不断取消。为什么?
有没有比zip + throw + catch更好的方法呢?我一直在搜索rxjs文档,但什么都没发现。
代码:
public getData(): Observable<Data> {
return Observable.zip(
this.userService.getCurrentUser(),
this.authHttp.get(this.adminUrl),
(user, adminResponse) => {
if (user.role == 'admin') {
return adminResponse.json() as BackendResponse
} else {
throw "You are not an admin";
}
})
.catch(error => {
return Observable.zip(
this.userService.getCurrentUser(),
this.authHttp.get(this.normalUrl),
(user, normalResponse) => {
if (user.role == 'normal') {
return normalResponse.json() as BackendResponse
} else {
throw "You are not allowed to get this resource";
}
})
})
.catch(error => {
return Observable.throw(error)
});
}
管理端点返回两个管理员和普通用户(需要在我的项目)的一些数据,但如果他们询问他们的特殊的API标准的用户可以得到更多的数据。
服务器应该拒绝任何未经授权的请求。 –