首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打2个时间较长的电话并将响应从第一个传递到第二个

打2个时间较长的电话并将响应从第一个传递到第二个
EN

Stack Overflow用户
提问于 2019-12-20 12:14:53
回答 2查看 78关注 0票数 0

我有一个使用Auth0的站点,用于确定用户是否是管理员--我在mongoDB数据库中为该用户存储了一个字段,并附上了他们的相关电子邮件,我从mongoDB()函数中的python端点中读取了这些邮件。但是,当用户登录时,我收到了Auth0用户电子邮件的响应,并将相关字段传递到getUser()函数中,但是,我想不出一个链接这些调用的解决方案。到目前为止,我已经尝试使用承诺和订阅,但没有效果。

web.service.ts

代码语言:javascript
运行
复制
getUser(email) {
        return this.http.get('http://localhost:5000/api/v1.0/user/' + email).subscribe(resp => {
            this.user_info = resp;
        });
    }

home.component.ts

代码语言:javascript
运行
复制
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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-20 12:26:13

代码语言:javascript
运行
复制
  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$方法。

票数 1
EN

Stack Overflow用户

发布于 2019-12-20 12:24:32

您可以在从Auth服务获得响应后执行您的链:即:

代码语言:javascript
运行
复制
  this.authService.userProfile$.subscribe(resp => {
      if (resp != null) {
        this.user_email = resp.email;
        this.getUser(this.user_email);
      }
  });

或者使用承诺和等待调用的组合,这样代码就会等待承诺得到解决,然后再继续向前。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59425110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档