[英]How to query for references using angularfire?
plans
是一個包含2個字段的根集合: 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的查詢,但這似乎是不可能的。 有什么建議么?
找到了:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.