我编写了以下查询来从 firestore 中提取文档。
await ctx.db
.collection("orderCollection")
.orderBy("timeStamp", "desc")
.orderBy("uuid")
.where(key, "==", ctx.uid)
.where("timeStamp", "<", parseInt(timeStamp))
.startAfter(lastResultUUID)
.withConverter(StoreOwnerROrderConvertor)
.limit(limitNum)
.get();
OrderCollection 包含有关所下订单的信息。 同时下多个订单。每个订单都有唯一的ID(UUID)。在每个页面上,我们将提取 limitNum 页。 lastResultUUID 包含上次提取的文档的 UUID。假设 limitNum=5。并且有 10 个订单同时下达,因此所有订单都有相同的时间戳。时间戳以毫秒为单位。在第 1 页上,我收到 5 个结果,lastResultUUID 为空,但在第 2 页上,我收到相同的 5 个结果。而lastResultUUID包含第1页第5项的UUID。我期待接下来的 5 个订单信息。
最佳答案
.startAfter()
将代码中的“lastResultUUID”与“timeStamp”进行比较。您必须按照与使用 .orderBy()
相同的顺序放置变量如下所示的字段:
.startAfter(lastResultTimeStamp, lastResultUUID)
或者直接传递整个 DocumentSnapshot<T>
:
.startAfter(lastDocumentSnapshot)
以防万一,您保存到数据库的文档不是 DocumentSnapshot<T>
!如果您不知道什么是 DocumentSnapshot<T>
,请阅读文档所以你知道要传递给像 .startAfter()
这样的方法.
关于javascript - 使用相同的时间戳 firestore 在不同页面上返回相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74915707/