使用开关映射的Angular firebase内部连接

问题描述 投票:0回答:1

我试图从firebase数据库中获取所有的订单,然后从订单中使用customerId获取客户的详细信息,这是我的数据库模式。

orders: customer customers customers: name surname

我的html模板。

  <h2>Raw Data</h2>
  <pre>{{rawData | async | json}}</pre>

以下代码返回 ERROR TypeError: "cyclic object value"

  constructor(public db: AngularFireDatabase) {
    this.rawData = db.list('/orders').valueChanges().pipe(
      switchMap(orders => { const customerIds = uniq(orders.map((ord: Order) => ord.customer));
      return customerIds.map(customerId => db.object('customers/' + customerId + '/').valueChanges());} )
    );
  }

但是,当我试图使用以下代码只返回一个客户,与第一个订单相关。

  constructor(public db: AngularFireDatabase) {
    this.rawData = db.list('/orders').valueChanges().pipe(
      switchMap(orders => { const customerIds = uniq(orders.map((ord: Order) => ord.customer));
      return db.object('customers/' + customerIds[0] + '/').valueChanges();} )
    );
  }

我收到了想要的输出.为什么在map中获取对我不起作用? 我需要进一步处理map的输出吗?理想情况下,我希望有这样的输出

{
order_param = order_value
customer = {customer_param = customer_value }
}

我是Angular的初学者,也是rxjs的初学者,我想按照这篇文章来做。 https:/medium.com@joaqcid how to-inner-join-data-from-multiple-collections-onangular-firebase-bfd04f6b36b7。

angular database typescript rxjs angularfire
1个回答
0
投票

发生这种情况是因为你返回了一个流的数组,但没有订阅它们。要做到这一点,你可以使用 合并所有.

正如文件所说。mergeAll subscribes to an Observable that emits Observables. Each time it observes one of these emitted inner Observables, it subscribes to that and delivers all the values from the inner Observable on the output Observable.

const getOrders$ = db.list('/orders').valueChanges();
const getCustomerChangesById = customerId => db.object('customers/' + customerId + '/').valueChanges()

constructor(public db: AngularFireDatabase) {
  this.rawData = getOrders.pipe(
    switchMap(orders => { 
      const customerIds = uniq(orders.map((ord: Order) => ord.customer));
      return customerIds.map(getCustomerChangesById);
    }),
    mergeAll(),
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.