简体   繁体   English

如何使用angularfire查询参考?

[英]How to query for references using angularfire?

plans is a root collection with 2 fields: date and recipe . plans是一个包含2个字段的根集合: daterecipe recipe is a reference to a different root collection called recipes . recipe是对另一个称为“ 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. 我尝试了this.db.doc(p[0].recipe) ,但是那没有返回可观察到的结果。 I looked at creating a query which specified multiple ids, but that doesn't seem possible. 我看着创建一个指定多个ID的查询,但这似乎是不可能的。 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM