Home > other >  How to create pagination using orderByChild, equalTo and endBefore from Firebase Query? React Native
How to create pagination using orderByChild, equalTo and endBefore from Firebase Query? React Native

Time:10-19

I am working in a pagination method where I want to load more data based on category type. Currently I am getting an Error:

endBefore: Starting point was already set (by another call to endAt, endBefore or equalTo).

I know I can't use endBefore and equalTo but I can't find a way to get what I want. If there is any approach to solve this will be amazing. Here is what I have of code.

function getPost() {
  const vibesQuery = query(
    vibesRef,
    orderByChild("category"),
    equalTo(categoryType),
    limitToLast(2)
  );
  onValue(vibesQuery, (snapshot) => {
    const data = snapshot.val();
    if (data) {
      const vibesArray = Object.values(data);
      setVibes(vibesArray.reverse());
      setLastVibe(vibesArray[vibesArray.length - 1][sortingType]);
    }
  });

function getMorePosts() {
    const vibesQuery = query(
      vibesRef,
      orderByChild("category"),
      equalTo(categoryType),
      endBefore(lastVibe),
      limitToLast(2)
    );
    onValue(vibesQuery, (snapshot) => {
      const data = snapshot.val();
      if (data) {
        const vibesArray = Object.values(data);
        setVibes([...vibes, ...vibesArray.reverse()]);
        setLastVibe(vibesArray[vibesArray.length - 1][sortingType]);
      }
      setIsMoreLoading(false);
    });
 }

My data structure is:

{
  "-LbzPjzin65Rt3ZIK1Lo": {
  "caption": "Great",
  "category": "OUTDOORS",
  "cityVibe": "Chino",
  "stateVibe": "CA",
  "creationDate": 1573942298.999069,
  "fullname": "Bryant",
 }, 
  "-LbzPjzin65Rt3ZIK1Io": {
  "caption": "Amazing",
  "category": "OUTDOORS",
  "cityVibe": "Chino",
  "stateVibe": "CA",
  "creationDate": 1576382057.7584639,
  "fullname": "Bravo",
 }, 
  "-LbzPjzin65Rt3ZIK1Ao": {
  "caption": "Beatiful",
  "category": "OUTDOORS", <-- THIS IS MY (categoryType)
  "cityVibe": "Chino",
  "stateVibe": "CA",
  "creationDate": 1586638159.889124, <-- THIS IS MY (lastVibe) 
  "fullname": "Bravo",
 },
  "-LbzPjzin65Rt3ZIK1Bo": {
  "caption": "Fantastic",
  "category": "OUTDOORS",
  "cityVibe": "Chino",
  "stateVibe": "CA",
  "creationDate": 1604361787.34916,
  "fullname": "Bravo",
 },
}

If there is any additional information, let me know and thank you so much!

CodePudding user response:

There is no way to pass multiple property values to filter on. What you can do in this case, it use Firebase's startAt/endBefore overload that takes two parameters. The first value is the property value you want to start/end at, but the second parameter is the key of the node you want to start/end at in case there are multiple matches on the first value.

So if you have the key of the last vibe in a variable called lastVibeKey, you can do:

query(
  vibesRef,
  orderByChild("category"),
  endBefore(categoryType, lastVibeKey),
  limitToLast(2)
);
  • Related