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

查看:20
本文介绍了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()

代码说明:从组中,将 groupIds 隔离到单独的数组中.然后并发调用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天全站免登陆