Querying Firebase by the value of a child node

Here is how my structure looks like:

2017511
   UcQefEaHJG6fteGsbsiaWjQ60d9Q62
     value1: 50
     value2: 1200
     value3: "blabla" 
   AcQefEaHJG6fteGsbsiaWjQ60d9Q62
     value1: 55
     value2: 2200
     value3: "balabla" 
   BcQefEaHJG6fteGsbsiaWjQ60d9Q62
     value1: 20
     value2: 500
     value3: "blabala" 

I am attempting to retrieve the value 3 of the top 10 highest value 2 entries. However, I encountered issues when new child nodes were added or values were changed.

$("#myList").html("");
firebase.database()
  .ref('/2017511')
  .limitToLast(2)
  .orderByChild("value2")
  .on("child_added", function(snapshot) {
      console.log(snapshot.val().value3 + " "+ snapshot.val().value2);
      $("#myList").prepend("<p>"+ snapshot.val().value2 + " "+ snapshot.val().value3 +"<p><br>");
  })

I tried a different approach that initially worked fine, but upon running the function again, it duplicated the same values in each column...

function yenile(){
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(1).once("child_added", function(snapshot) {
$("#bugun1").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
// More lines of similar code for different elements up to bugun10...
}

Answer №1

It was confirmed by the original poster that the solution provided below was successful.

function yenile(){ 
   var i = 10 
   firebase.database().ref('/'+today).orderByChild("esita").lim‌​itToLast(5).on("chil‌​d_added", function(snapshot) { 
        $("#bugun"+i).html(snapshot.val().username 
         + " (" + (snapshot.val().esita) + ")"); i -= 1; }); 
}

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

How can a server retrieve a file uploaded using FormData and Ajax on a cross-domain upload?

my website is running on localhost:8084 and I need to upload a file to localhost:8086. Below is the JavaScript code: var xhr = new XMLHttpRequest(); xhr.open("post", "http://localshot:8086"+ "?type=ajax",true); xhr.setRequestHeader("X-Reque ...

Please enter a numerical value into the input field in a JavaScript form

<script> function loop() { var input = document.getElementById('inputId').value; for (var i = 0; i < input; i++) { var result = document.getElementById('outputDiv').innerHTML ...

What is the best way to fetch images from a JSON object in a React application?

I have been trying to load images from an array of objects in React, but I keep encountering a "module not found" error. This issue has been frustrating me for the past couple of days. Can someone please help me troubleshoot this problem or provide some su ...

Can a Stylus and Node.js with Express project utilize a local image?

Let's talk about using images in a Web app. In my Stylus file, style.styl, I typically set the image using code like this: .background background: url(http://path/to/image) But what if we want to save the image to our local app directory and use ...

Slightly puzzled by the declaration of `var app = express()`

After going over the documentation, I'm still struggling to grasp why we store express() inside an app variable. I attempted to call methods using express().get and .post directly, but was unsuccessful. Why is that? Why doesn't it function in t ...

Stop Internet Explorer from automatically scrolling when it gains focus

Please access this fiddle using internet explorer (11): https://jsfiddle.net/kaljak/yw7Lc1aw/1/ When the page loads, the <p> tag is focused and Internet Explorer slightly scrolls the element so that the table border becomes invisible... document.qu ...

Apologies, we encountered an issue while attempting to display the Firebase projects. For additional information, please refer to

While working on creating a Firestore web app, I encountered an issue in the 3rd lesson with the npm code below. I used the command firebase use --add to list the projects and received the following error message in the console log: **[debug] [2021-03-0 ...

``There seems to be an issue with the functionality of Passport.js' multiple login system

I'm encountering a strange issue with my login system. Everything seems to be working fine, including local login, Google login, and Facebook login. However, the problem arises when I attempt to register with Google after already registering with Face ...

Steps to simulate a TouchEvent programmatically using TypeScript

Is there a way to manually trigger the touch event in TypeScript? In JavaScript, it was possible but I am struggling to achieve the same in TypeScript. For example: let touchStart: TouchEvent = document.createEvent('TouchEvent'); touchStart.i ...

What could be causing my form not to Submit when attempting submission without utilizing the submit button?

Here is the code for my validation: $(function () { function validateForm() { // Code to validate form inputs } $('#myform').submit(validateForm); }); After this, I want to submit the form when a certain action occurs: < ...

Trouble with mouseout listener in Wordpress causing Google Map to malfunction

Having trouble integrating a map into my WordPress site using the Google Maps API v3. The issue I am facing is that the listener assigned to the mouseout event is not functioning properly. I have copied and pasted the code from another website where it was ...

Tips on creating type definitions for CSS modules in Parcel?

As someone who is brand new to Parcel, I have a question that may seem naive. In my project, I am using typescript, react, less, and parcel. I am encountering an error with typescript stating 'Cannot find module 'xxx' or its corresponding t ...

What is the best way to transfer a variable from PHP to JavaScript as the actual variable itself, rather than as a string?

I am transferring data from PHP to JavaScript using AJAX. I apply json_encode before sending the data. Here is an example: $data = [ [ "path" => "/TestMenu1", "component" => 'test1', "children" => [[ ...

Guide on displaying the name attribute of a button along with its price in a separate div when clicked

Upon clicking the Koala button, I want to display a message showing the id name of the button and the corresponding price for Koalas. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link h ...

Leveraging AJAX for transmitting form information to a php script without relying on jQuery

I want to explore AJAX by implementing a basic form data submission to a php script and database. For educational purposes, I've reached a standstill after hitting the "Create Profile" button with no action. Could someone spot any syntax or structural ...

What is the best way to retrieve the canonicalized minimum and maximum values within the beforeShow method of jQuery UI's datepicker?

I have a pair of datepicker elements that I want to function as a range. When a value is set in the "low" datepicker, it should adjust the minDate and yearRange in the other datepicker, and vice versa with the "high" datepicker. The challenge I'm faci ...

Creating a loop in a column-based carousel without using JavaScript

I'm currently working on developing a carousel using JavaScript or jQuery. I've attempted the code snippet below, but I'm having trouble getting it to display in 3 or 4 columns. While I understand that Bootstrap can handle this easily, I&apo ...

Personalized AngularJS required text

After utilizing the built-in AngularJS directive required and encountering a validation error, I receive a small popup near the field displaying "Please fill out this field". My issue lies in needing the text to be displayed in another language. How should ...

Tips for minimizing the transfer time of large arrays using ajax

https://i.stack.imgur.com/IP0oe.pngDescription I am currently working on transferring a JSON object from the server to the client using PHP and JavaScript via AJAX. The JSON object contains a large array (200x200) of integers. The server is running on lo ...

The PHP blocking code in Zend Server not only blocks the response of the current ajax call but also impacts the handling

I encountered a peculiar problem. Suppose I have an ajax call like this; $.ajax({ url:"url1.php", }) Following this ajax call, I have another ajax call as follows; $.ajax({ url:"url2.php", success:function(data){console.log(data);} }) ...