简体   繁体   English

如何使用javaScript在Firebase数据库中检索嵌套的子值?

[英]How to retrieve nested child value in Firebase database using javaScript?

Here is my Fireabse database structure. 这是我的Fireabse数据库结构。 I want to retrieve data of 20170116 keys that is not a hard coded value. 我想检索不是硬编码值的20170116密钥的数据。 It's dynamic keys. 这是动态键。 I got some keys and values like : 我得到了一些键和值,如:

This is my funtion : 这是我的功能:

function getData(prospectId) {
    database.ref('users/'+prospectId).once('value').then(function(snapshot) {
        var prospectId = snapshot.key ;
        console.log("prospectId : "+ prospectId); // output is : prospectId : 1104812

        snapshot.forEach(function(childSnapshot) {
            var businessUrl = childSnapshot.key;
            console.log("businessUrl : "+ businessUrl); // output is : businessUrl : http:\\www.abc.com
            var dates = Object.keys(childSnapshot.val());
            console.log("dates : "+ dates); //output is : dates : 20170116,20170117,20170119,20170121
            var singleDate = dates[0];
            console.log("singleDate : "+ singleDate); //output is : singleDate : 20170116
        });
    });
}  

getData(1104812);

这是我的Fireabse数据库结构

So how to get 20170116 date data or snapshot ? 那么如何获得20170116日期数据或快照?

You're attaching a value listener to /users/1104812 . 您正在将值侦听器附加到/users/1104812 So the snapshot you get in your callback will contain the child nodes under that: 20170116 , 20170117 and 20170119 . 所以,你在你的回调得到快照将包含在该子节点: 201701162017011720170119

When you loop over the children (with snapshot.forEach(function( ) your childSnapshot will become each of those nodes in turn. 当您遍历子项时(使用snapshot.forEach(function( ),您的childSnapshot将依次成为每个节点)。

None of these nodes has a child clientUrl or districtId , those are one level deeper into the tree: 这些节点都没有子clientUrldistrictId ,这些节点在树中更深一层:

database.ref('users/'+prospectId).once('value').then(function(snapshot) {
  var prospectId = snapshot.key ;

  snapshot.forEach(function(snapshot1) {
    console.log(snapshot1.key); // e.g. "http://..."
    snapshot.forEach(function(snapshot2) {
      console.log(childSnapshot.key); // e.g. "20170116"
      childSnapshot.forEach(function(snapshot3) {
        console.log(grandchildSnapshot.key); // e.g. "-Kb9...gkE"
        console.log(grandchildSnapshot.val().districtId); // "pne"
      });
    });
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检索子值并使用 firebase 与 javascript 进行比较 - Retrieve an child value and compare using firebase by javascript 如何在Firebase实时数据库中过滤和检索嵌套子级? - How to filter and retrieve nested child in Firebase realtime database? 如何使用Firebase云功能从Firebase数据库检索子节点的键值? - How to retrieve key value of child node from firebase database using firebase cloud function? 如何使用 javascript 从 firebase 中的子项中检索子项值? - How to retrieve child value from child in firebase with javascript? Firebase 实时:如何在 Firebase 数据库 javascript 中更新子项的特定值 - Firebase realtime:How to update particular value of child in Firebase Database javascript 如何使用 Javascript 检索实时数据库中的值? - How to retrieve a value in Realtime Database using Javascript? 如何使用JavaScript从Firebase实时数据库中检索对象数组? - How to retrieve an array of objects from a firebase realtime database using javascript? 如何将数据上传到 Firebase 数据库并使用 javascript 在日期的帮助下检索它 - How to Upload Data to Firebase Database and Retrieve it by help of date using javascript 使用 Javascript(父、子 + 父、子、子)从 firebase 数据库中读取值 - read value from firebase database using Javascript(parent,child + parent,child,child) 如何使用 firebase 数据库存储和检索数组值? - How can i stored and retrieve an array value using firebase database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM