Home > Software engineering >  How Can I Pagination Next and Previous With Firebase Database Without createdAt
How Can I Pagination Next and Previous With Firebase Database Without createdAt

Time:12-04

There is no createdAt column in the database.

I'm trying to pagination, but it's not working properly.

When I press the previous button endBefore(firstElement) When I press the next button startAfter(lastElement)

There is no problem going forward, but the page order is broken when coming back.

How can I solve this?

const queryConstraints = []
if(onlyMain) queryConstraints.push(where("mainpost","==",true))
if(postType === "previous") queryConstraints.push(endBefore(visibleObject))
if(postType === "next") queryConstraints.push(startAfter(visibleObject))


 query(collection(db,"posts"),orderBy("title"),where("title", ">=", "Demo"),limit(count),...queryConstraints)

CodePudding user response:

If you're browsing backwards, you need to use limitToLast instead of limit. With limitToLast the query returns the documents before the cursor object that you specify, while using limit returns the ones after it.

  • Related