I am trying to paginate queries and then populate my FlatList to enable infinite scrolling.
In the index.d.ts -> This is the function structure:
/**
* Creates and returns a new Query that starts after the provided document
* (exclusive). The starting position is relative to the order of the query.
* The document must contain all of the fields provided in the orderBy of
* this query.
*
* @param snapshot The snapshot of the document to start after.
* @return The created Query.
*/
startAfter(snapshot: DocumentSnapshot): Query;
/**
* Creates and returns a new Query that starts after the provided fields
* relative to the order of the query. The order of the field values
* must match the order of the order by clauses of the query.
*
* @param fieldValues The field values to start this query after, in order
* of the query's order by.
* @return The created Query.
*/
startAfter(...fieldValues: any[]): Query;
With the above in mind I am first saving the last doc.data() and then taking a field from it and then again getting more data from firebase in onEndReached function of the FlatList
const feedsCollection = firebase.firestore().collection("Feeds").limit(2);
const lastVisibleSnapshot = feedsSnapshot.docs[feedsSnapshot.docs.length - 1];
const lastVisible = lastVisibleSnapshot.data();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I save this to redux
And then in onEndReached I am doing this:
const nextFeeds = firebase.firestore().collection("Feeds").startAfter(lastVisible.videoURL).limit(25);
I chose videoURL since that is going to be unique. But it throws an error:
Too many arguments provided to startAfter(). The number of arguments must be less than or equal to the number of orderBy() clauses.
I understand that I have supply orderBy() with the query. But is there a way where I can do it without it?
Any solution?
@kk1429 Unfortunately not. This is a limitation of the underlying SDK which enforces that you need to specify an orderBy field to use the startAfter, startAt, endAt and endBefore methods.
Most helpful comment
@kk1429 Unfortunately not. This is a limitation of the underlying SDK which enforces that you need to specify an
orderByfield to use thestartAfter,startAt,endAtandendBeforemethods.