我使用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
......
})
答案 0 :(得分:3)
事实证明,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使其按预期工作
答案 1 :(得分: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);
}
}