使用RxJ和AngularFirestore通过外键获取单个文档 [英] Get a single document by foreign key in using RxJs and AngularFirestore

查看:55
本文介绍了使用RxJ和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.

这篇关于使用RxJ和AngularFirestore通过外键获取单个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆