javascript - How can I get all the child nodes from a parent node in Firebase? - Stack Overflow

So I'm intending of retrieving a list of nodes from the parent node societies which will store the

So I'm intending of retrieving a list of nodes from the parent node societies which will store the child node names, retrieved from this case being ALT5 , ASN11 , Foot15

The child node directly beneath sUsers is the uid of that user so it can be retrieved using var runUid = firebase.auth().currentUser.uid; so that the query can work with multiple users with different child nodes

EDIT : This needs to work on other nodes within sUsers that will have different child nodes under Societies

code so far, currently because there are 3 child nodes under societies it will log 3 null values, whereas I want the actual names of the child nodes stored

    firebase.auth().onAuthStateChanged(function(user){
   var userID = firebase.auth().currentUser.uid;
   var rootRef = firebase.database().ref('sUsers');
   var newRoot = rootRef.child(userID).child('Societies')
    newRoot.once('value', function(snapshot){
        snapshot.forEach(function(childs){
            var societies = childs.child('Societies').val();
            console.log(societies);

        });
    });
});

So I'm intending of retrieving a list of nodes from the parent node societies which will store the child node names, retrieved from this case being ALT5 , ASN11 , Foot15

The child node directly beneath sUsers is the uid of that user so it can be retrieved using var runUid = firebase.auth().currentUser.uid; so that the query can work with multiple users with different child nodes

EDIT : This needs to work on other nodes within sUsers that will have different child nodes under Societies

code so far, currently because there are 3 child nodes under societies it will log 3 null values, whereas I want the actual names of the child nodes stored

    firebase.auth().onAuthStateChanged(function(user){
   var userID = firebase.auth().currentUser.uid;
   var rootRef = firebase.database().ref('sUsers');
   var newRoot = rootRef.child(userID).child('Societies')
    newRoot.once('value', function(snapshot){
        snapshot.forEach(function(childs){
            var societies = childs.child('Societies').val();
            console.log(societies);

        });
    });
});
Share Improve this question edited Apr 9, 2018 at 15:01 shaddles asked Apr 9, 2018 at 12:22 shaddlesshaddles 1411 gold badge4 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

In the code you posted, the variable childs you're using in DataSnapshot.forEach is actually each child of the node 'Societies'. So this should work:

firebase.auth().onAuthStateChanged(function(user){
    var userID = firebase.auth().currentUser.uid;
    var rootRef = firebase.database().ref('sUsers');
    var newRoot = rootRef.child(userID).child('Societies');
    newRoot.once('value', function(snapshot){
        snapshot.forEach(function(_child){
            var society = _child.key;
            console.log(society);
        });
    });
});

Here you can see some documentation and some examples.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信