我有一个使用Auth0的站点,用于确定用户是否是管理员--我在mongoDB数据库中为该用户存储了一个字段,并附上了他们的相关电子邮件,我从mongoDB()函数中的python端点中读取了这些邮件。但是,当用户登录时,我收到了Auth0用户电子邮件的响应,并将相关字段传递到getUser()函数中,但是,我想不出一个链接这些调用的解决方案。到目前为止,我已经尝试使用承诺和订阅,但没有效果。
web.service.ts
getUser(email) {
return this.http.get('http://localhost:5000/api/v1.0/user/' + email).subscribe(resp => {
this.user_info = resp;
});
}home.component.ts
export class HomeComponent {
constructor(private authService: AuthService,
private webService: WebService) {}
user_email;
is_admin;
ngOnInit() {
this.authService.userProfile$.subscribe(resp => {
if (resp != null) {
this.user_email = resp.email
}
}); //after this aync call is completed, I want to pass the user_email into the getUser()
//function and set is_admin depending on the response
}发布于 2019-12-20 12:26:13
this.authService.userProfile$.pipe(switchMap((resp) => this.webService.getUser(resp.email))).subscribe((resp) => {
this.is_admin = resp.is_admin;
});
getUser(email) {
return this.http.get('http://localhost:5000/api/v1.0/user/' + email)
} // Dont subscribe here to compose as done above您可以使用运算符从一个流组合到另一个流。在这里,我将userProfile$流映射为getUser()流。我在这里使用switchMap操作符,如果userProfile$流在userProfile$处于进度状态时发出值,它将取消userProfile$方法。
发布于 2019-12-20 12:24:32
您可以在从Auth服务获得响应后执行您的链:即:
this.authService.userProfile$.subscribe(resp => {
if (resp != null) {
this.user_email = resp.email;
this.getUser(this.user_email);
}
});或者使用承诺和等待调用的组合,这样代码就会等待承诺得到解决,然后再继续向前。
https://stackoverflow.com/questions/59425110
复制相似问题