How to retrieve nested child value in Firebase database using javaScript? - Stack Overflow

Here is my Fireabse database structure. I want to retrieve data of 20170116 keys that is not a hard cod

Here is my Fireabse database structure. I want to retrieve data of 20170116 keys that is not a hard coded value. 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
            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);

So how to get 20170116 date data or snapshot ?

Here is my Fireabse database structure. I want to retrieve data of 20170116 keys that is not a hard coded value. 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.
            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);

So how to get 20170116 date data or snapshot ?

Share edited Jan 28, 2017 at 18:25 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Jan 28, 2017 at 12:04 MenuMenu 6851 gold badge14 silver badges32 bronze badges 1
  • Educated guess below. If that's not it (and for future questions) please replace the picture of JSON with the actual JSON as text, which you can easily get by clicking the Export JSON link in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Commented Jan 28, 2017 at 18:32
Add a ment  | 

1 Answer 1

Reset to default 5

You're attaching a value listener to /users/1104812. So the snapshot you get in your callback will contain the child nodes under that: 20170116, 20170117 and 20170119.

When you loop over the children (with snapshot.forEach(function() your childSnapshot will bee each of those nodes in turn.

None of these nodes has a child clientUrl or districtId, those are one level deeper into the tree:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098541a4611132.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信