最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论