创建一个包含Behavioursubject flat的可观察对象

时间:2016-11-30 21:34:39

标签: javascript angular rxjs observable

我做了一个在我的应用程序中注册用户的功能。为此,我写了以下代码:

onRegister() {
    const data = {
      firstName: this.form.value.firstName,
      lastName: this.form.value.lastName,
      emailAddress: this.form.value.emailAddress
    };

    this.userService.getUserByEmail(data)
      .map(response => {
        const authData = {
          uid: response.uid,
          newUser: response.newUser
        };
        return authData;
      })
      .do(authData => this.accountService.addUsersToAccount(authData, data))
      .map(authData => {
        if (authData.newUser) {
          return this.userService.createUser(data, authData, 5);
        } else {
          return this.accountService.addAccountToUser(data, authData, 5);
        }
      })
      .subscribe((resp: Observable<User>) => {
        console.log('added user to account', resp);
      });
}

在我的订阅功能中,我想在console.log中添加用户。目前,resp是一个可观察的,持有一个具有所需值的Behavioursubject。 resp的结构如下:

将用户添加到帐户:Observable

- source: Observable
-- source: BehavoirSubject
--- value: Object  (<<<< this is the data I want in my console.log)

1 个答案:

答案 0 :(得分:0)

您的createUser返回Observable of Users。您可以使用flatMap()代替map()来平展此内容并让用户加入subscribe