Retrieving data from same FireBase tables but in a different paths

时间:2018-01-30 15:12:51

标签: javascript firebase firebase-realtime-database

My FireBase structure is shown below:

enter image description here

enter image description here

I need to retrieve data from different FireBase tables, In order to append the data in a single html table, every FireBase table(node) is a column in my html table.

Is there a way to do this in one single query? Instead of writing it in a sloppy way like a 30 identical queries?

The only change between the all FireBase tables is the path in the middle:

var ref = firebase.database (). ref ("dailyT /Avg_Gain/" + desiredDate) .orderByChild ("Prefix") 

var ref = firebase.database (). ref ("dailyT /Buy_Date/" + desiredDate) .orderByChild ("Prefix")

var ref = firebase.database (). ref ("dailyT /Buy_Price/" + desiredDate) .orderByChild ("Prefix")

Thank you very much.

            var ref = firebase.database().ref("dailyT/" + Various tables +  "+desiredDate).orderByChild("Prefix")) //.orderByChild("Prefix")
                                ref.once("value").then(function(snapshot) {
                                        if(snapshot.exists()){
                                            snapshot.forEach(function(childSnapshot) {  
                                  var val = childSnapshot.val();

                                                console.log("Prefix: "+val.Prefix);


                                        }); 

                                         // Append to table code
                                    }else{          
                                            //No such data in the firebase
                                    }
                                });

1 个答案:

答案 0 :(得分:0)

您可以先检查root用户的密钥,如:

var keys = [];
var db = firebase.database();
db.ref().once('value', s => {
       s.forEach(k=> {
         keys.push(k.key); // result:[Avg_Gain, Buy_Date, Buy_Price...]
       })
})

然后您可以将相同的desiredDate数据检索到上述键中。

vals= [];
    keys.forEach(k=>{
      db.ref('dailyT/'+k+"/" +desiredDate).orderByChild("Prefix")
        .once('value', snap=>{
        if (snap.exists()) { vals.push(snap.value()) }
      }) 
})