plans
是具有两个字段的根集合:date
和recipe
。 recipe
是对另一个名为recipes
的根集合的引用。我正在尝试构建一个可观察的链,该链发出计划在指定日期范围内引用的食谱。
lookup(range: MealPlanRange): Observable<Recipe[]> {
return this.db.collection('plans', ref=>ref
.where('date', ">=", range.startDate )
.where('date', "<=", range.endDate )
).valueChanges().pipe(
// at this point, i have the plans i want,
// but i don't know how to get the recipes
switchMap(ps=>(/*how to get observable of recipes?*/)),
);
}
我尝试了this.db.doc(p[0].recipe)
,但是没有返回可观察到的结果。我看着创建一个指定多个ID的查询,但这似乎是不可能的。有什么建议吗?
答案 0 :(得分:0)
找到了:
lookup(range: MealPlanRange): Observable<Meal[]> {
return this.db.collection('plans', ref => ref
.where('date', ">=", range.startDate)
.where('date', "<=", range.endDate)
).valueChanges().pipe(
switchMap(ps => {
// return empty array when no plans
if(ps.length === 0) {
return of([])
}
// for each plan, do a doc lookup and use valueChanges() to get observable of changes
const recipes = ps.map(p => this.db.doc(p.recipe).valueChanges());
// use combineLatest to get back into an array of recipes,
// any change to any recipe will re-emit
return combineLatest(...recipes);
}),
) as any;
}