首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角火/火基>获取集合,并为每个文档获取其子集合

角火/火基>获取集合,并为每个文档获取其子集合
EN

Stack Overflow用户
提问于 2020-11-26 01:36:58
回答 2查看 133关注 0票数 0

我有一个集合的市场,每个市场都有一个产品的集合。我正试着查询以得到所有的..。最后,我得到了一个解决方案,但是我在第9行得到了关于compineLatest的提示:"@deprecated参数在一个数组中“。

我想知道是否有更好的and,以及如何正确地使用compineLatest。

代码语言:javascript
复制
  //#region fetch method
  private fetch(marketsRef: AngularFirestoreCollection<Market>) {
    return marketsRef
      .snapshotChanges()
      .pipe(
        switchMap(markets => {
          if (!markets.length) {
            this._noDataSubject$.next(true);
          }
          return combineLatest([markets],
            combineLatest(
              markets.map(market => {
                return this.firestore.collection<MarketProduct>(`markets/${market.payload.doc.id}/products`, ref => {
                  return ref.orderBy('reviewResult')
                })
                  .snapshotChanges()
              })
            )
          )
        }), map(([markets, products]) => {
          const allProducts = products.reduce((acc, val) => acc.concat(val), []);
          const returnedMarkets = markets.map(market => {
            return {
              id: market.payload.doc.id,
              ...market.payload.doc.data() as Market,
              products: allProducts
                .filter(product => product.payload.doc.ref.path === `markets/${market.payload.doc.id}/products/${product.payload.doc.id}`)
                .map(product => {
                  return {
                    id: product.payload.doc.id,
                    ...product.payload.doc.data() as MarketProduct
                  }
                })
            }
          }) as MarketId[];
          return returnedMarkets;
        }))
  }
  //#endregion fetch method

  //#region fetch  new markets request
  fetchNewMarkets() {
    const key = `name.ar`;
    const marketsRef = this.firestore.collection<Market>('markets', ref => {
      return ref.where('reviewResult', '==', null).orderBy(key);
    });
    this.fetch(marketsRef)
      .subscribe(markets => {
        this._marketsChanges$.next(markets);
      })
  }
  //#endregion fetch new markets request
EN

回答 2

Stack Overflow用户

发布于 2020-11-26 08:01:03

代码语言:javascript
复制
return combineLatest([markets], combineLatest(/* ... */));

你有这条线。第一个combineLatest有两个参数。不推荐使用签名combineLatest(...obs)。您所需要做的就是将第二个combineLatest添加到数组中。

代码语言:javascript
复制
return combineLatest([markets, combineLatest(/* ... */)]);

编辑,附加建议

代码语言:javascript
复制
// const allProducts = products.reduce((acc, val) => acc.concat(val), []);
const allProducts = products.flat();
票数 1
EN

Stack Overflow用户

发布于 2020-11-28 19:41:49

我的终端代码

代码语言:javascript
复制
//#region fetch method
  private fetch(marketsRef: AngularFirestoreCollection<Market>) {
    return combineLatest([
      marketsRef.snapshotChanges(),
      marketsRef.snapshotChanges()
        .pipe(
          switchMap(markets => {
            if (!markets.length) {
              this._noDataSubject$.next(true);
            }
            return combineLatest(
              markets.map(market => {
                return this.firestore.collection<MarketProduct>(`markets/${market.payload.doc.id}/products`, ref => {
                  return ref.orderBy('reviewResult')
                })
                  .snapshotChanges()
              })
            )
          })

        )]).pipe(map(([markets, products]) => {
          const allProducts = products.reduce((acc, val) => acc.concat(val), []);
          return markets.map(market => {
            return {
              id: market.payload.doc.id,
              ...market.payload.doc.data() as Market,
              products: allProducts
                .filter(product => product.payload.doc.ref.path === `markets/${market.payload.doc.id}/products/${product.payload.doc.id}`)
                .map(product => {
                  return {
                    id: product.payload.doc.id,
                    ...product.payload.doc.data() as MarketProduct
                  }
                })
            }
          }) as MarketId[];
        }));
  }
  //#endregion fetch method

  //#region fetch  new markets request
  fetchNewMarkets() {
    const key = `name.ar`;
    const marketsRef = this.firestore.collection<Market>('markets', ref => {
      return ref.where('reviewResult', '==', null).orderBy(key);
    });
    this.fetch(marketsRef)
      .subscribe(markets => {
        this._newMarketsChanges$.next(markets);
      })
  }
  //#endregion fetch new markets request
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65014988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档