Go from Array of FirebaseListObservables to Array of Strings

时间:2017-12-18 04:58:02

标签: angular typescript firebase rxjs angularfire2

EDITED TO INCLUDE MORE DETAIL:

Let's assume I have a collection of Survey objects in Firebase, and a collection of SurveyTaker objects in Firebase, and a surveyTakersBySurvey relationship setup like

+-- surveyTakersBySurvey
   |
   +-- survey1
   |    |
   |    +-- surveyTaker1 = true
   |
   +-- survey2
        |
        +-- surveyTaker1 = true

This code currently works

findSurveyByState(surveyState:string): Observable<Survey> {
    return this.db.list('surveys', {
        query: {
            orderByChild: 'state',
            limitToFirst: 1,
            equalTo: surveyState
        }
    })
    .map(results => results[0]);
}

findSurveyTakerKeysPerSurveyState(surveyState:string,
                           query: FirebaseListFactoryOpts = {}): Observable<string[]> {
    return this.findSurveyByState(surveyState)
        .do(val => console.log("survey",val))
        .filter(survey => !!survey)
        .switchMap(survey => this.db.list(`surveyTakersBySurvey/${survey.$key}`,query))
        .map( stsbs => stsbs.map(stbs => stbs.$key) );
}

I need to change the above code to work for multiple surveys returned with the same state.

I'm struggling with rxjs programming...

//i first changed this to return multiple
findSurveysByState(surveyState:string): Observable<Survey[]> {
    return this.db.list('surveys', {
        query: {
            orderByChild: 'state',
            equalTo: surveyState
        }
    })
    //.map(results => results[0]);
}

// this is the method that is not working
findSurveyTakerKeysPerSurveyState(surveyState:string,
                           query: FirebaseListFactoryOpts = {}): Observable<string[]> {
    //need to update to take multiple Surveys
    return this.findSurveysByState(surveyState)
        .do(val => console.log("surveys",val))
        .filter(survey => !!survey)
        //.switchMap(survey => this.db.list(`surveyTakersBySurvey/${survey.$key}`,query))
        // switchMap would only make sense for one at a time?
        .map(surveys => surveys.map(survey => this.db.list(`surveyTakersBySurvey/${survey.$key}`, query)))
        // on the below line i get [ts] Property '$key' does not exist on type 'FirebaseListObservable<any[]>'.
        .map( stsbs => stsbs.map(stbs => stbs.$key) );
}

I get an [undefined, undefined]

Any help would be appreciated. Let me know if i need to elaborate on anything important.. i'm still feeling my way around here.

EDIT: this.db is from ... import { AngularFireDatabase } from "angularfire2/database";

1 个答案:

答案 0 :(得分:2)

使用combineLatest

findSurveyTakerKeysPerSurveyState(surveyState:string,
    query: FirebaseListFactoryOpts = {}): Observable<string[]> {
  //need to update to take multiple Surveys
  return this.findSurveysByState(surveyState)
  .do(val => console.log("surveys",val))
  .filter(survey => !!survey)
  //.switchMap(survey => this.db.list(`surveyTakersBySurvey/${survey.$key}`,query))
  // switchMap would only make sense for one at a time?
  .switchMap(surveys => {
    let observables = [];
    surveys.forEach(survey => {
      observables.push(this.db.list(`surveyTakersBySurvey/${survey.$key}`, query));
    });
    return Observable.combineLatest(observables);
  })
  .map( stsbs => stsbs.map(stbs => stbs.$key) );
}

您可能需要导入这两行。

import 'rxjs/add/observable/combineLatest';
import { Observable } from 'rxjs/Observable';