angularfire2 - 如何使用 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;
}
推荐阅读
- c# - 127.0.0.1 不起作用,但 localhost 工作正常
- c# - 无法从 api .net 核心获得任何响应
- laravel - 带有 Laravel 缓存问题的 Owl Carousel
- javascript - 如何在我的 http 调用中按特定日期范围排序?
- flutter - 使用 jumpto 和 sticky_headers 包会导致 UI 问题
- java - 生日钱计算器
- git - 为什么从詹金斯的工作A触发工作B不会重新结帐,以及如何强制它?
- python-3.x - 这是发出后请求登录网站的正确方法吗
- c++ - 读取阻塞进程的输出
- java - 从 ActiveMQ 并行消费消息