使用 RxJs 和 AngularFirestore 通过外键获取单个文档 [英] Get a single document by foreign key in using RxJs and AngularFirestore
本文介绍了使用 RxJs 和 AngularFirestore 通过外键获取单个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个看起来像这样的查询:
I've written a query that looks like this:
getPaymentByBookingId(id: string): Observable<Payment> {
return this.afs.collection<Payment>('payments', ref => ref.where('bookingId', '==', id).limit(1))
.valueChanges()
.pipe(
flatMap(array => from(array)),
first()
);
}
其中 Payment
对象具有对 Booking
id 的唯一引用.我想查找给定 ID 的付款.
Where a Payment
object has a unique reference to a Booking
id. I want to find the Payment for a given ID.
这似乎是纠正此查询的一种非常复杂的方法.
This seems like quite a convoluted way to right this query.
有没有更简单的方法来做到这一点 - 包括这里是否有代码异味使这变得困难?
Is there a simpler way to do this - including if there is a code smell here that makes this hard?
推荐答案
稍微简单一点的是:
getPaymentByBookingId(id: string): Observable<Payment> {
return this.afs.collection<Payment>('payments', ref => ref.where('bookingId', '==', id).limit(1))
.valueChanges()
.pipe(
map(array => array[0]),
);
}
如果未找到付款,这将返回 undefined
.
this will returned undefined
if the payment is not found.
这篇关于使用 RxJs 和 AngularFirestore 通过外键获取单个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文