Angular 使用切换映射的 firebase 内部连接
Angular firebase inner join using switch map
我正在尝试从 firebase 数据库中获取所有订单,然后使用订单中的 customerId 获取有关客户的详细信息。
这是我的数据库模式:
订单:
顾客
客户:
名字
姓
我的 html 模板:
<h2>Raw Data</h2>
<pre>{{rawData | async | json}}</pre>
以下代码 returns 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());} )
);
}
但是,当我尝试 return 只有一个客户时,与第一个订单相关,使用以下代码:
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();} )
);
}
我收到了想要的输出。
为什么获取地图对我不起作用?我需要进一步处理地图的输出吗?
理想情况下,我希望有这样的输出
{
order_param = order_value
customer = {customer_param = customer_value }
}
我是 Angular 和 rxjs 的初学者,我正在尝试阅读这篇文章:
https://medium.com/@joaqcid/how-to-inner-join-data-from-multiple-collections-on-angular-firebase-bfd04f6b36b7
它正在发生,因为您是 return 一组流但没有订阅它们。为此,您可以使用 mergeAll.
如文档所述,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(),
);
}
我正在尝试从 firebase 数据库中获取所有订单,然后使用订单中的 customerId 获取有关客户的详细信息。 这是我的数据库模式:
订单:
顾客
客户:
名字
姓
我的 html 模板:
<h2>Raw Data</h2>
<pre>{{rawData | async | json}}</pre>
以下代码 returns 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());} )
);
}
但是,当我尝试 return 只有一个客户时,与第一个订单相关,使用以下代码:
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();} )
);
}
我收到了想要的输出。 为什么获取地图对我不起作用?我需要进一步处理地图的输出吗? 理想情况下,我希望有这样的输出
{
order_param = order_value
customer = {customer_param = customer_value }
}
我是 Angular 和 rxjs 的初学者,我正在尝试阅读这篇文章:
https://medium.com/@joaqcid/how-to-inner-join-data-from-multiple-collections-on-angular-firebase-bfd04f6b36b7
它正在发生,因为您是 return 一组流但没有订阅它们。为此,您可以使用 mergeAll.
如文档所述,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(),
);
}