gpt4 book ai didi

angular - 从 FirebaseListObservables 数组到字符串数组

转载 作者:太空狗 更新时间:2023-10-29 18:30:13 27 4
gpt4 key购买 nike

编辑以包含更多详细信息:

假设我在 Firebase 中有一个 Survey 对象集合,在 Firebase 中有一个 SurveyTaker 对象集合,以及一个 surveyTakersBySurvey 关系设置,例如

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

这段代码目前有效

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) );
}

我需要更改上面的代码以处理以相同状态返回的多个调查。

我正在为 rxjs 编程而苦苦挣扎......

//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) );
}

我得到一个[undefined, undefined]

如有任何帮助,我们将不胜感激。如果我需要详细说明任何重要的事情,请告诉我。我仍在摸索。

编辑:this.db 来自 ... import { AngularFireDatabase } from "angularfire2/database";

最佳答案

使用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';

关于angular - 从 FirebaseListObservables 数组到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com