javascript - 如何知道在实现分页时 Firestore 查询将返回多少个项目

标签 javascript firebase google-cloud-firestore

Firestore 提供了有关如何对查询进行分页的指南:

Firestore - Paginate data with query cursors

他们展示了以下示例:

Paginate a query

Paginate queries by combining query cursors with the limit() method. For example, use the last document in a batch as the start of a cursor for the next batch.

var first = db.collection("cities")
       .orderBy("population")
       .limit(25);

return first.get().then(function (documentSnapshots) {
 // Get the last visible document
 var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
 console.log("last", lastVisible);

 // Construct a new query starting at this document,
 // get the next 25 cities.
 var next = db.collection("cities")
         .orderBy("population")
         .startAfter(lastVisible)
         .limit(25);
});

问题

我得到了示例,但是我如何知道查询将返回多少个项目(总共,没有限制)?我需要它来计算页数并控制分页组件,不是吗?

在不知道限制的情况下,我无法简单地显示下一个和后退按钮。

应该怎样做?我错过了什么吗?

最佳答案

你无法提前知道结果集的大小。您必须翻阅所有结果才能获得总大小。这类似于无法知道集合的大小而不在其他地方记录自己 - 它只是无法以 Cloud Firestore 需要扩展的方式扩展来提供此信息。

关于javascript - 如何知道在实现分页时 Firestore 查询将返回多少个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690432/

相关文章:

javascript - 为什么另一个对象中的 Parent.call(this) 可以工作?

javascript - 如何动态启用/禁用响应式扩展

java - 如何限制 Firebase 电子邮件登录中的错误密码尝试?

java - Firebase Firestore 在运行时错误时崩溃

firebase - Flutter Firestore甚至在提供orderby函数后仍以错误的顺序获取

javascript - 获取对附加项目的引用并将一些其他项目附加到先前附加的项目

javascript - 在 Java 中的 MongoDB $where 子句中使用 lambda 函数

android - 将 ServerValue.TIMESTAMP 转换为日期

java - 如何仅在 map 上的半径(圆)内显示标记?

firebase - 如何查看 Cloud Firestore 日志?