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/