首页 > 解决方案 > Waiting for dynamically generated Firebase list observables to finish

问题描述

I'm trying to wait for several dynamically generated observables to finish so I can iterate through Firebase lists. I've tried with both forkJoin and zip but the last line doesn't log anything.

this.db.list<ChatByUser>(`chats_by_user/${this.userService.user.id}`).valueChanges().first().subscribe(chatsByUser => {
    const obsArray: Observable<Chat>[] = [];

    chatsByUser.forEach(chatByUser => {
        obsArray.push(this.db.object<Chat>(`chats/${chatByUser.chatId}`).valueChanges());
    });

    Observable.zip(obsArray).first().subscribe(chats => console.log(chats));
});

I don't know if it has something to do with it, but I'm using rxjs 6 with rxjs-compat and importing the operators like this:

import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/zip";

标签: angularfirebasefirebase-realtime-databaserxjsangularfire2

解决方案


在将它们添加到数组之前,我需要使用这些 observables。所以添加first()方法就可以valueChanges()了。


推荐阅读