2

我正在使用 forkJoin 来组合两个 firebase 请求的结果

两个请求都完成并在控制台中记录,但 forkJoin 本身的 map 函数不会触发,因此不会向应用程序返回任何结果

public initGroup(groupname, username){
  console.log(groupname, username)//This logs
  return Observable.forkJoin([
      this.getGroup(groupname, username),
      this.groupMembers(username, groupname),
    ])
    .map((data)=>{
      console.log(data)//This does not log
      this.group = data;
      return this.group
    })
}

对于各个功能:

public getGroup(groupname, username){
  return (this._af.database.object('/groups/'+groupname) as FirebaseObjectObservable<any>)
      .map((group)=>{
       console.log(group)//This logs
       return group
  })

}
 public groupMembers(username, groupname){
  return  this.afService.getUserItems(groupname)
  .map((users:UserInfo[])=>{
    console.log(users)//This logs
    return users
  })
}

我在组件内订阅:

let conn = this.groupService.initGroup(groupname, username)
                  .subscribe((data)=>{
                  console.log(data)//Does not log
                  ......
                })
4

2 回答 2

4

事实证明 forkJoin 不适用于 firebase observables,

当我更新到

public initGroup(groupname, username){
  return Observable.combineLatest([
      this.getGroup(groupname, username),
      this.groupMembers(username, groupname)

  ])
    .map((data)=>{
      console.log(data)//This now logs
      this.group = data;
      return this.group
    })


}

combineLatest 使其按预期工作

于 2017-07-05T17:54:31.113 回答
1

这是我的用例Firestore

 import { Observable } from 'rxjs/Observable';
 import { combineLatest } from 'rxjs/observable/combineLatest';

 constructor((){}

getAllBudgetGroups() {
    const loading = this.loadingProvider.presentLoader();
    try {
     this.budgetGroups$ = this.budgetGroupProvider.getAllTempBudgetGroups().valueChanges();
     this.defaultBudgetGroups$ = this.budgetGroupProvider.getDefaultBudgetGroups().valueChanges();
     combineLatest(this.budgetGroups$, this.defaultBudgetGroups$).subscribe(([res1, res2]) => {
        this.budgetGroupSortedList = concat(res1, res2);
        this.loadingProvider.dismissLoader(loading);
     }, err => {
        console.log(err);
        this.loadingProvider.dismissLoader(loading);
      });
    }
    catch (err) {
      console.log(err);
      this.loadingProvider.dismissLoader(loading);
    }
  }
于 2018-02-16T00:44:30.920 回答