I would like to filter results by a certain child with equalTo
but also want to paginate the results. I followed this answer but I’m getting this error:
startAfter: Starting point was already set (by another call to startAt, startAfter, or equalTo).
Checking the source code, it looks like equalTo
basically sets both startAt
and endAt
and because I tried to set startAt
again, it’s erroring out.
Is it because of my Firebase Admin SDK version? I’m using v9.4.2.
Here’s my code:
query = query.orderByChild("category").equalTo(filter);
if (lastKey) {
query = query.startAfter("", lastKey);
}
2
Answers
The problem was as I already said in the question,
equalTo
andstartAt
orstartAfter
cannot be used at the same time becauseequalTo
under the hood calls bothstartAt
andendAt
with the same value.Logically also, it doesn't make sense to find something that is "equal to some value and greater than another value".
So, my solution was to use
startAt
andendAt
with the same filter value, but adding thelastKey
value to thestartAt
clause so that it can paginate.Here's my code:
You can only have a single
startAt
/startAfter
/equalTo
call in a query on the Firebase Realtime Database.If your
startAfter
is acategory
value (i.e. the same value asfilter
, you’re looking for:If your
startAfter
is the key of a node with the same category asfilter
, then you’re looking for:The
~
is not some magic operator, but just a character high in the ASCII range.