最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to combine Firestore orderBy desc with startAfter cursor - Stack Overflow

programmeradmin1浏览0评论

i am trying to query a list in firestore that should be sorted by a descending date property and make use of a startAfter cursor to paginate the results.

As you can see in the snippets below, this is failing once i combine orderBy('date', 'desc') with startAfter(lastDoc.date).

I am wondering what i am doing wrong. Any ideas?

// 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()

i am trying to query a list in firestore that should be sorted by a descending date property and make use of a startAfter cursor to paginate the results.

As you can see in the snippets below, this is failing once i combine orderBy('date', 'desc') with startAfter(lastDoc.date).

I am wondering what i am doing wrong. Any ideas?

// 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()

Share Improve this question edited Jan 4, 2018 at 15:20 Frank van Puffelen 599k84 gold badges888 silver badges858 bronze badges asked Jan 4, 2018 at 14:46 user2458046user2458046 4551 gold badge4 silver badges14 bronze badges 1
  • those who are looking for explanation -> link – Rohan majhi Commented Sep 4, 2022 at 20:33
Add a comment  | 

2 Answers 2

Reset to default 15

You need to pass the actual document snapshot to startAfter, not the date value:

db.collection('tanks')
  .doc(tankId)
  .collection('documentations')
  .orderBy('date', 'desc')
  .startAfter(lastDoc)
  .limit(pageSize)
  .get()

See the reference documentation for startAfter().

This is actually working, no idea why it did not before...

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();

发布评论

评论列表(0)

  1. 暂无评论