使用 valueChanges() 时获取分页的最后文档参考

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

我在 AngularFire 中使用 valueChanges() 来订阅 Firestore 数据,并且需要获取最后的文档引用以进行分页。 我之前使用 onSnapshot 与

.onSnapshot((querySnapshot) => {
this.lastV = qSnapshot.docs[qSnapshot.docs.length - 1]
}

但这不适用于 valueChanges()。我怎样才能实现这一点,我想检索对最后一个文档的引用并将其传递到startAfter(lastV)

.valueChanges().subscribe(qItems => {
        if (Array.isArray(qItems) && qItems.length > 0) {
          const lastDoc = qItems[qItems.length - 1];
          this.lastV = this.firestore.doc(`${this.getPath()}/${lastDoc.uuid}`);
angular typescript firebase google-cloud-firestore pagination
1个回答
0
投票

我能想到的唯一方法是添加一些时间戳,例如

date
,然后进行相应的查询,例如:

  constructor(private afs: AngularFirestore) {
   this.items = afs.collection<Item>('items', ref => ref.orderBy('date', 'desc').limit(1));
   this.items.valueChanges().pipe(
      map(actions => {
        return actions.map(a => {
            const data = a['payload'].doc.data() as Item;
            return {...data };
        });
      }

使用

orderBy
能够获取最后一个文档,并使用
limit
将其限制为您想要的文档数量

© www.soinside.com 2019 - 2024. All rights reserved.