'Joining' Firebase Queries in Angularfire2

前端 未结 2 961
旧巷少年郎
旧巷少年郎 2021-01-02 20:12

Update:

The issues I was encountering with the empty value fields had to do with non-existent keys in my database, so most of the discourse here won\'t apply to yo

2条回答
  •  感动是毒
    2021-01-02 20:59

    You can compose an observable based on getFeaturedThreads that queries members and replaces the values in each thread's participants property with user names:

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/forkJoin';
    import 'rxjs/add/operator/do';
    import 'rxjs/add/operator/first';
    import 'rxjs/add/operator/switchMap';
    
    let featuredThreadsWithUserNames = this.getFeaturedThreads()
    
      // Each time the getFeaturedThreads emits, switch to unsubscribe/ignore
      // any pending member queries:
    
      .switchMap(threads => {
    
        // Map the threads to the array of observables that are to be
        // joined. When the observables emit a value, update the thread.
    
        let memberObservables = [];
        threads.forEach(thread => {
    
          // Add the author:
    
          memberObservables.push(this.af.database
            .object(`members/${thread.author}`)
            .first()
            .do(value => { thread.author = value.username; })
          );
    
          // Add the participants:
    
          Object.keys(thread.participants).forEach(key => {
            memberObservables.push(this.af.database
              .object(`members/${key}`)
              .first()
              .do(value => { thread.participants[key] = value.username; })
            );
          });
        });
    
        // Join the member observables and use the result selector to
        // return the threads - which will have been updated.
    
        return Observable.forkJoin(...memberObservables, () => threads);
      });
    

    This will give you an observable that emits each time getFeaturedThreads emits. However, if the user names change, it won't re-emit. If that's important, replace forkJoin with combineLatest and remove the first operator from the composed member observables.

提交回复
热议问题