plans
is a root collection with 2 fields: date
and recipe
. recipe
is a reference to a different root collection called recipes
. I'm trying to construct an observable chain which emits recipes referenced by plans for the specified date range.
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?*/)),
);
}
i tried this.db.doc(p[0].recipe)
, but that doesn't return an observable. I looked at creating a query which specified multiple ids, but that doesn't seem possible. Any suggestions?
Found it:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.