首页 > 解决方案 > 使用 Firebase 逐个获取元素

问题描述

我想在 Firebase 中获得一个子值,但是我不知道该怎么做。

例子:

/users/{some_user_id_here}/{rank_id_here}
/users/1866/4

例如,当您的 rank id 为 4 时,我需要获取所有用户。

标签: firebasefirebase-realtime-database

解决方案


以下应该可以解决问题:

        var userQuery = firebase.database().ref('users').orderByChild('rank').equalTo(4);
        userQuery.once('value', function(snapshot) {
            snapshot.forEach(function(childSnapshot) {
              var childKey = childSnapshot.key;
              var childData = childSnapshot.val();
              console.log(childKey);
              console.log(childData);
           });
        });

看看这里的文档:https ://firebase.google.com/docs/database/web/lists-of-data#listen_for_value_events


推荐阅读