UPDATED FOR MORE DETAIL:
Imagine I have a collection of Survey objects and SurveyTaker objects in Firebase, with a relationship set up as follows:
+-- surveyTakersBySurvey
|
+-- survey1
| |
| +-- surveyTaker1 = true
|
+-- survey2
|
+-- surveyTaker1 = true
The code provided is currently functioning as expected.
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 now need to modify the above code to handle multiple surveys returned with the same state.
I am facing challenges with rxjs programming...
// Modified to return multiple surveys
findSurveysByState(surveyState:string): Observable<Survey[]> {
return this.db.list('surveys', {
query: {
orderByChild: 'state',
equalTo: surveyState
}
})
//.map(results => results[0]);
}
// Method that needs updating
findSurveyTakerKeysPerSurveyState(surveyState:string,
query: FirebaseListFactoryOpts = {}): Observable<string[]> {
// Update to handle 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)))
// Error on line below: Property '$key' does not exist on type 'FirebaseListObservable<any[]>'.
.map( stsbs => stsbs.map(stbs => stbs.$key) );
}
I am receiving an [undefined, undefined] output.
Any assistance would be greatly appreciated. Please let me know if further clarification is required. Thank you!
NOTE: this.db is imported from { AngularFireDatabase } from "angularfire2/database";