按子值查询Firebase

时间:2017-05-11 10:54:48

标签: javascript firebase firebase-realtime-database

我的结构就像

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

我试图获得前10名最高值2的值3 我试过这样的事情但是当新的孩子添加或任何值改变时它会搞砸

$("#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>");
  })

我尝试了这个,它最初工作正常,但如果我再次运行函数,它只是在每列中粘贴相同的值...

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) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(2).once("child_added", function(snapshot) {
$("#bugun2").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(3).once("child_added", function(snapshot) {
$("#bugun3").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(4).once("child_added", function(snapshot) {
$("#bugun4").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(5).once("child_added", function(snapshot) {
$("#bugun5").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(6).once("child_added", function(snapshot) {
$("#bugun6").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(7).once("child_added", function(snapshot) {
$("#bugun7").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(8).once("child_added", function(snapshot) {
$("#bugun8").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(9).once("child_added", function(snapshot) {
$("#bugun9").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
firebase.database().ref('/'+today).orderByChild("esita").limitToLast(10).once("child_added", function(snapshot) {
$("#bugun10").html(snapshot.val().username + " (" + (snapshot.val().esita).toFixed(0) + ")");
});
}

1 个答案:

答案 0 :(得分:0)

OP确认如下工作。

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