简体   繁体   English

分页(Startafter)在firestore和Angular 9 App中不起作用

[英]Pagination (Startafter) is not working in firestore and Angular 9 App

On page load I'm getting the data of 15 records, after scrolling down I can able to call the API but it's returning empty array.(I have total 40 records in my firestore db).在页面加载时,我得到了 15 条记录的数据,向下滚动后,我可以调用 API 但它返回的是空数组。(我的 Firestore 数据库中共有 40 条记录)。

// Service.ts
getUsers(config: any) {
    return this.firestore
    .collection(
      'users',
      ref => ref
      .where('country', '==', 'India')
      .orderBy('lastlogin')
      .startAfter(config.page * config.limit)
      .limit(config.limit)
    )
    .snapshotChanges();
  }

// Component

getUsers() {
    this.loader = true;
    this.userService.getUsers(this.config).subscribe((res: any) => {
      this.loader = false;
      const data = res.map((e: any) => ({
        id: e.payload.doc.id,
        ...e.payload.doc.data()
      }));
      this.usersCollection = this.usersCollection.concat(data);
    });

// Infinite Scroll
onScroll() {
    this.config.page += 1;
    this.getUsers();
  }

My Solution我的解决方案

Only problem is first time lastVisible have no data, have to set to 0.唯一的问题是第一次 lastVisible 没有数据,必须设置为 0。

getUsers(config: any, lastVisible) {
    const { doc } = lastVisible.payload ? lastVisible.payload : { doc: 0}
    return this.firestore
    .collection(
      'users',
      ref => ref
      .where('country', '==', 'India')
      .orderBy('lastlogin')
      .startAfter(doc)
      .limit(config.limit)
    )
    .snapshotChanges();
  }

// Component

getUsers() {
    this.loader = true;
    this.userService.getUsers(this.config, this.lastVisible).subscribe((res: any) => {
      this.loader = false;
      this.lastVisible = res[res.length - 1];
      const data = res.map((e: any) => ({
        id: e.payload.doc.id,
        ...e.payload.doc.data()
      }));
      this.usersCollection = this.usersCollection.concat(data);
    });

Firestore pagination does not work based on a numeric offset, but is based on so-called cursors/anchor document. Firestore 分页不是基于数字偏移量,而是基于所谓的游标/锚文件。 From the documentation on startAfter :startAfter的文档中:

Creates and returns a new Query that starts after the provided document (exclusive).创建并返回在提供的文档(不包括)之后开始的新查询。 The starting position is relative to the order of the query.起始 position 与查询的顺序有关。 The document must contain all of the fields provided in the orderBy of this query.该文档必须包含此查询的 orderBy 中提供的所有字段。

So your call:所以你的电话:

 .startAfter(config.page * config.limit)

Tries to start after the document instance config.page * config.limit .尝试在文档实例config.page * config.limit之后启动。 Since this is not a document snapshot/reference but a number, it doesn't know what document to start after, so it returns nothing.由于这不是文档快照/参考,而是一个数字,它不知道从哪个文档开始,所以它什么也不返回。

Instead of passing a numeric offset, you'll need to remember the last document snapshot of the current results and pass that into startAfter .您需要记住当前结果的最后一个文档快照并将其传递给startAfter ,而不是传递数字偏移量。

I highly recommend reading the documentation on paginating data with query cursors .我强烈建议阅读有关使用查询游标对数据进行分页的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM