使用forEach从firebase检索子节点值

时间:2016-06-27 20:59:41

标签: javascript json firebase firebase-realtime-database

我已经开始学习网页开发,但是我对如何从Firebase检索标准数据感到困惑,但我想在密钥的每个子节点中访问更多信息。

Example: "destination", "distance" inside each  "2016-06-04 06:11:40" keys

我可以使用以下行检索部分数据:

function initApp() {    
     .........
     .........

        var myDatesKeys = ' ';

        var query = firebase.database().ref('users/' + uid +'/waypoints/Work/2016/06').orderByKey();

        query.once("value")
          .then(function(snapshot) {
            snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key;
            var childData = childSnapshot.val();

            myDatesKeys += key + '.........';

            });

        $("#demo").text(myDatesKeys);
}); 

JSON结构:

{
  "users" : {
    "erhg8aCN57YHtaZWPCDMQfkctfw2" : {
      "email" : "sune@hotmail.com",
      "waypoints" : {
        "Private" : {
          "2016" : {
            "06" : {
              "2016-06-04 06:11:40" : {
                "destination" : "Skandiavägen 6A, 451 43 Uddevalla, Sweden",
                "distance" : "1,3 m",
                "duration" : "1 min",
                "encodedLatLngs" : "gbacJaqugA@?",
                "key" : "2016-06-04 06:11:40",
                "origin" : "Skandiavägen 6A, 451 43 Uddevalla, Sweden",
                "timeStamp" : "2016-06-04 06:11:40"
              },
              "2016-06-04 11:45:04" : {
                "destination" : "44, Uddevalla, Sweden",
                "distance" : "2.4 km",
                "duration" : "3 mins",
                "encodedLatLngs" : "_adcJiyjgAxNjlDsCvk@T|FwBlX",
                "key" : "2016-06-04 11:45:04",
                "origin" : "44, Uddevalla, Sweden",
                "timeStamp" : "2016-06-04 11:45:04"
              }
            }
          }
        },
        "Work" : {
          "2016" : {
            "06" : {
              "2016-06-04 11:37:22" : {
                "destination" : "Västerlånggatan 10, 451 31 Uddevalla, Sweden",
                "distance" : "0.9 km",
                "duration" : "1 min",
                "encodedLatLngs" : "ctccJanygAF_@?]Ka@FeDn@sQjF`A|AnKsFtI}@Bw@QW_@OMSDKPGZ?^HZPRPCLM",
                "key" : "2016-06-04 11:37:22",
                "origin" : "Västerlånggatan 10, 451 31 Uddevalla, Sweden",
                "timeStamp" : "2016-06-04 11:37:22"
              },

0 个答案:

没有答案