Observable.forkJoin()不执行

The*_*eal 16 observable rxjs rxjs5 angularfire2 angular

我有以下代码:

//Loop: For each user ID/Role ID, get the data
userMeta.forEach((businessRole) => {
  Observable.forkJoin(
    af.database.object('/roles/'+businessRole.$value),
    af.database.object('/users/'+businessRole.$key)
  ).subscribe(
    data => {
      console.log("Data received");
      data[1].role = data[0];
      this.users.push(data[1]);
    },
    err => console.error(err)
  );
Run Code Online (Sandbox Code Playgroud)

我试图订阅使用2个observable的结果forkJoin.

由于某些原因,未显示"已接收数据"消息.

我的userMeta变量在console.log中看起来很好:

在此输入图像描述

怎么了?

更新:以下代码也不返回任何内容

let source = Observable.forkJoin(
        af.database.object('/roles/'+businessRole.$value),
        af.database.object('/users/'+businessRole.$key)
    );
    let subscription = source.subscribe(
      function (x) {
    console.log("GOT: " + x);
  },
  function (err) {
    console.log('Error: %s', err);
  },
  function () {
    console.log('Completed');
  });
Run Code Online (Sandbox Code Playgroud)

我实际上要做的是提高以下代码的性能:

//Subscription 3: role ID to role Name
        af.database.object('/roles/'+businessRole.$value)
        .subscribe((roleData) => {
        //Subscription 4: Get user info
        af.database.object('/users/'+businessRole.$key).subscribe(user => {
Run Code Online (Sandbox Code Playgroud)

mar*_*tin 19

forkJoin() 要求所有源Observable至少发出一次并完成.

以下演示按预期完成:

const source = forkJoin(
  from([1,2,3]),
  from([9,8,7,6])
).subscribe(
  x => console.log('GOT:', x),
  err => console.log('Error:', err),
  () => console.log('Completed')
);
Run Code Online (Sandbox Code Playgroud)

现场演示:https://stackblitz.com/edit/rxjs-urhkni

GOT: 3,6
Completed
Run Code Online (Sandbox Code Playgroud)

2019年1月:更新了RxJS 6

  • `Observable.combineLatest()`可能就是你正在寻找的@TheUnreal. (7认同)
  • @TheUnreal 但这与你想用 `forkJoin()` 做的事情不同。`.subscribe((roleData) => {...` 在每个 `next` 调用时发出一个值。`forkJoin()`。**forkJoin() 需要两个 Observables 完成。** 否则它不会发出任何东西. (2认同)

ans*_*sen 13

我有一个类似的问题使用Angular 2/Angularfire 2,特别是在我查询用户是否通过电子邮件存在的问题.在一种情况下,用户存在,我从Observable接收了一个对象的数组.在另一种情况下,用户不存在,我收到一个空数组.

当我使用带有resultSelector和subscribe的forkJoin时,resultSelector和subscribe函数都没有运行.但是,当我尝试

Observable.zip(
  FirebaseListObservable,
  FirebaseListObservable,
  (...results) => {
    return results.map(some code here)
  }
).subscribe(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)

选择器和订阅都工作.我认为这与@ martin的答案有关,其中forkJoin需要observables来完成,因为根据定义它返回最后的排放.如果一个观察者永远不会完成,我想它永远不会有最后一次发射.

也许angularfire列表observables(或你的情况下的object observable)永远不会完成,使得forkJoin的使用变得不可能.幸运的是,zip具有类似的行为并且仍然有效,不同之处在于,如果Firebase中的数据发生更改,它可以重复多次,而forkJoin只会结合最后一个响应.

就我而言,我正在寻找1)使用zip并接受我的代码可能会运行多次如果用户数据在.zip仍在运行时发生变化,2)在第一组数据返回后手动禁用zip,或者3)抛弃Angularfire并直接尝试Firebase api,使用像.once这样的东西来查看是否可以获得完成并触发forkJoin的observable.

  • 我有完全相同的问题,可以通过使用`Observable.combineLatest`而不是`Observable.forkJoin`来解决这个问题.积分转到https://github.com/angular/angularfire2/issues/617 (6认同)

bgo*_*ndy 13

我遇到了类似的问题:我正在动态创建一个observable列表,我注意到forkjoin()如果observable列表为空,则永远不会发出也不会完成,而Promise.all()使用空列表解析:

Observable.forkJoin([])
    .subscribe(() => console.log('do something here')); // This is never called
Run Code Online (Sandbox Code Playgroud)

我找到的解决方法是检查列表的长度,并在它为空时不使用此运算符.

return jobList.length ? Observable.forkJoin(jobList) : Observable.of([]);
Run Code Online (Sandbox Code Playgroud)


Dáv*_*oly 11

我遇到了同样的问题,我无法真正让forkJoin操作员工作,所以我只是使用了combineLatest,它已经起作用了!


Ita*_*uel 6

只需添加observer.complete();

不管用:

observer.next(...)
Run Code Online (Sandbox Code Playgroud)

将工作:

observer.next(...);
observer.complete();
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。