forkjoin returns no results

2019-02-26 05:45发布

I'm using forkJoin to combine the results of two firebase requests

Both requests complete and log within the console, but the map function for the forkJoin itself does not fire and hence no results are returned to the application

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
    })
}

And for the individual functions:

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
  })
}

I subscribe within the component:

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

2条回答
Bombasti
2楼-- · 2019-02-26 06:05

This is my use case with 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);
    }
  }
查看更多
时光不老,我们不散
3楼-- · 2019-02-26 06:15

It turns out forkJoin just doesn't work with firebase observables,

When I updated to

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 made it work as expected

查看更多
登录 后发表回答