AngularFire Rxjs订阅-返回零结果 [英] AngularFire Rxjs subscription - return nill results

查看:55
本文介绍了AngularFire Rxjs订阅-返回零结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历两个集合,首先获取用户组,然后遍历每个组并获取与该组关联的作业.效果很好,但是问题是,如果没有作业,应用程序将处于加载状态.

I have two collections which I need to loop through, I start by geting the users groups, I then loop through each group and get the jobs associated with that group. This works perfectly, but the problem is that if there is no jobs the app stays in a loading state.

这是我的代码:

this.fb
  .getUsersGroupsAsObservable(user.uid, groupType) // first get the users groups
  .subscribe(groups => {
    combineLatest( // for each group get the jobs belonging to that group
      groups.map(group => this.fb.getJobsbyGroup(group.id)),
    ).subscribe(res => { // if there is no results this wont execute
      this.jobs = [].concat.apply([], res);
    });
  });

理想情况下,如果我可以确定getJobsbyGroup不返回任何结果并返回一个空数组,那将是很好的.抱歉,如果措辞不好,我对这种情况下所需的术语并不完全自信.

Ideally it would be good if I could determine that the getJobsbyGroup is not returning any results and return an empty array. Sorry if this isnt worded well I'm not totally confident on the terminology needed in this case.

推荐答案

您可以检查这是否是您想要的吗? 链接. 这是要点.您可以检查this.fb.getJobsbyGroup是否有结果,然后返回结果,否则返回未定义.

Can you check whether this is what you are looking for? link. This is the gist. you can check whether this.fb.getJobsbyGroup has result, then return result else return undefined.

  this.fb.getUsersGroupsAsObservable(user.uid, groupType)
.pipe(map(groups => groups.map(group => group.id)),
switchMap(groupIds => from(groupIds).pipe(mergeMap(id => this.fb.getJobsbyGroup(id).pipe(map(jobs => jobs ? jobs : []))),toArray())))
.subscribe()

代码说明:从这些组中,将groupId隔离到单独的数组中.然后并发调用getJobsbyGroup并检查结果是否存在或返回空数组.然后将其结果合并到一个单独的数组中并返回它.让我知道您是否需要这个.

Code Explanation: From the groups, isolate the groupIds into separate array. Then concurrently call getJobsbyGroup and check whether the result is there or return empty array. Then merge their results into a separate array and return it. Let me know whether this what you needed.

这篇关于AngularFire Rxjs订阅-返回零结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆