Learn how to implement pagination in your React Native app by utilizing the orderByChild, equalTo, and endBefore functions in a Firebase

I am currently developing a pagination feature that loads more data based on category type. However, I have encountered an issue:

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

I understand that I can't use endBefore and equalTo together, but I am struggling to find a solution. If anyone has a workaround for this, I would greatly appreciate it. Here is the code snippet I have:

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);
    });
 }

Here is the structure of my data:

{
  "-LbzPjzin65Rt3ZIK1Lo": {
  "caption": "Great",
  "category": "OUTDOORS",
  "cityVibe": "Chino",
  "stateVibe": "CA",
  "creationDate": 1573942298.999069,
  "fullname": "Bryant",
 }, 
  ...
}

If you have any additional insights or suggestions, please feel free to share. Thank you for the assistance!

Answer №1

When it comes to passing multiple property values to filter on, there is a limitation. However, a workaround for this is to utilize Firebase's startAt/endBefore overload that allows for two parameters. The first parameter is the property value to start/end at, while the second parameter represents the key of the node to start/end at in cases where there are multiple matches on the first value.

For instance, if you have the key of the last vibe saved in a variable named lastVibeKey, you can implement the following:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for a way to update template variables in copy using grunt?

I am currently utilizing load-grunt-config along with grunt-contrib-copy. My objective is to have the copy task replace certain template tags using the 'process' option. I understand that replacing template tags is feasible based on the grunt-co ...

How come attempting to read a nonexistent document from Firestore results in an uncaught promise?

I've been struggling to read and display data from Firestore, but I keep seeing error messages in the console. Encountered (in promise) a TypeError: Unable to read properties of undefined (reading 'ex') Encountered (in promise) a TypeError ...

Transferring the chosen dropdown value to the following page

I am attempting to transfer the currently selected value from a dropdown to the next page using an HTTP request, so that the selected value is included in the URL of the subsequent page. The form dropdown element appears as follows: <form name="sortby ...

What strategies can I use to incorporate dynamic filtering using only AJAX while maintaining a functional browsing history?

As I work on implementing AJAX filtering for my e-commerce website, I am exploring different solutions. One approach I am considering involves generating all content statically server-side and then using AJAX requests on the same page with parameters. The ...

JavaScript: Remove just the specific user input without affecting the rest of the HTML block

I'm facing a dilemma with deleting a specific user input without removing the entire HTML block. I know that the delete button triggers on the HTML ID 'noteDelete', which is the parent of each user input. However, I'm unsure about how t ...

Vue-router vulnerability allowing for DOM-based open redirects

I am currently working on a Vue application that was created using Vue-cli. Vue version: 2.6.11 vue-router version: 3.2.0 Link for Reproduction https://github.com/keyhangholami/dom-based-open-redirect Instructions to replicate To reproduce the i ...

Efficient Checkbox Implementation in ActiveAdmin for Rails 3.1

This query actually encompasses two related inquiries. Enabling "Select all" functionality - In the realm of formtastic, which is used by Active_admin to render forms, how can I implement a button that selects all checkboxes present on the page? While ...

Ways to eliminate HTML elements from displayed content

I need help removing the <p> tags from comment text that is rendered. When passing the content to a component as a prop, I am experiencing issues with the v-html directive not working correctly. How can I render the content without the HTML tags? C ...

Gremlin, can you tell me where the "valueMap()" function has been imported from in JavaScript?

Working with ES6 on Node.js, I am attempting to execute the project() step within a Gremlin query. For this projection, my goal is to extract the properties. In the Gremlin console, I would typically use valueMap() to retrieve these properties. However, ...

What is preventing the function from waiting for the promise to be resolved?

I am encountering an issue with the code snippet below. The control does not wait for the HTTP promise to be resolved before returning a string from the method, and I can see that the returned object is "method" in the switch statement. Can someone please ...

Having trouble getting the Babel plugin transform-remove-console to function properly with the Vue CLI 4 and @vue/cli-plugin-babel/preset?

When you create a VueJS project using Vue CLI 4, Babel is configured with a useful preset in babel.config.js: module.exports = { presets: [ '@vue/cli-plugin-babel/preset', ], }; I am attempting to use babel-plugin-transform-remove-conso ...

What is the best choice for managing state storage and retrieval when working with React, React Router, and React Relay?

As I navigate through my app using React, React Router, and React Relay, I encounter different ways of handling data updates. React offers useState, React Router has its pushState wrapper (location.state), and Relay utilizes the updater() method. However, ...

Encountering the error message "Function.prototype.bind.apply(...) cannot be invoked as a constructor

I'm attempting to implement Controller Inheritance in AngularJS (1.6.9), but encountering an error in the console: Function.prototype.bind.apply(...) is not a constructor. Here's the snippet from the HTML file: <!-- Controller Inheritance --& ...

Struggling with extracting an array of objects from a JSON file and accessing individual attributes within each object?

As a newcomer to Typescript, I am eager to work with JSON and access its objects and attributes. However, I am encountering difficulties in achieving this. I have attempted using functions like 'for of' and 'for in', but unfortunately, ...

Troubleshooting: The issue with applying 'style' in react-draft-wysiwyg

Is there a way to style my textboxes within a rectangle with a default height? I attempted to use the style attribute in my <Editor /> but it didn't work. import { Editor } from "react-draft-wysiwyg"; import { EditorState } from " ...

VueJS emits a warning when filtering an array inside a for loop

I'm encountering an issue with my filtering function in VueJS. While it works fine, I am seeing a warning message in the console. You can check out this example to see the problem. The problem arises when I need to filter relational data from a separ ...

Error: Attempting to access the 'url' property of an undefined variable, despite specifically checking for its undefined status

Within my React application, I am utilizing the following state: const [functions, setFunctions] = useState([{}]); I have created a test to check if a specific property is undefined: if (typeof functions[functionCount].url !== "undefined") { ...

Efficiently updating database records without the need for page reloads using EJS

I'm working on a project that resembles the comment section on Reddit. Users can leave comments and others can reply to those comments. My tech stack includes Node, Express, MySQL, and EJS. The issue I'm facing is implementing the upvote/downvo ...

Sending data from frontend to backend in a Node.js Express application

My Node.js Express project in the WebStorm IDE is structured like this: https://i.sstatic.net/pQKwb.jpg I'm trying to find a way to pass a variable from index.js to app.js so that I can write it to data.json at the end. As a beginner in Node.js, I& ...

Can you explain the distinction between these two concepts in JavaScript?

I've come across an issue while assigning PHP values to JavaScript. var a = <?php echo 39; ?> JavaScript is throwing an error that says "Uncaught SyntaxError: Unexpected token ILLEGAL". However, when I assign PHP values in a slightly different ...