我正在尝试查询firestore中应该按降序日期属性排序的列表,并使用startAfter游标对结果进行分页。
正如您在下面的代码段中所看到的,一旦我将orderBy('date','desc')与startAfter(lastDoc.date)结合起来,这就失败了。
我想知道我做错了什么。有什么想法吗?
// this actually works
// but it is sorted by ascending dates
db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date')
.startAfter(lastDoc.date)
.limit(pageSize)
.get()
// this even works...
// but has no cursor (startAfter) set for pagination
db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date', 'desc')
.limit(pageSize)
.get()
// this is what i need
// but it returns always zero results
db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date', 'desc')
.startAfter(lastDoc.date)
.limit(pageSize)
.get()
答案 0 :(得分:3)
您需要将实际文档快照传递给startAfter
,而不是日期值:
db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date', 'desc')
.startAfter(lastDoc)
.limit(pageSize)
.get()
答案 1 :(得分:1)
这实际上是有效的,不知道为什么之前没有......
const snapshot = lastDoc
? await Api.db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date', 'desc')
.startAfter(lastDoc.date)
.limit(pageSize)
.get()
: await Api.db.collection('tanks')
.doc(tankId)
.collection('documentations')
.orderBy('date', 'desc')
.limit(pageSize)
.get();