skip to Main Content

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


  1. Chosen as BEST ANSWER

    The problem was as I already said in the question, equalTo and startAt or startAfter cannot be used at the same time because equalTo under the hood calls both startAt and endAt 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 and endAt with the same filter value, but adding the lastKey value to the startAt clause so that it can paginate.

    Here's my code:

    query = query.orderByChild("category").startAt(filter, lastKey).endAt(filter);
    

  2. You can only have a single startAt/startAfter/equalTo call in a query on the Firebase Realtime Database.

    If your startAfter is a category value (i.e. the same value as filter, you’re looking for:

    query = query.orderByChild("category").startAt(startAfter).endAt(filter+'~');
    

    If your startAfter is the key of a node with the same category as filter, then you’re looking for:

    query = query.orderByChild("category").startAt(filter, startAfter).endAt(filter+'~');
    

    The ~ is not some magic operator, but just a character high in the ASCII range.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search