Firebase查询显示键值,如果为true

时间:2017-07-19 22:45:45

标签: javascript firebase firebase-realtime-database

尝试安排group2个成员,因此在这种情况下结果应为user2,并且不会显示它是否存在:

{
  "users":{
    "user1":{
        "username":"john",
        "groups":{
            "group1":true,
            "group3":true
        }
    },
    "user2": ...,
  }
  "groups": {
     "group1"{
        ...
      },
     "group2"{
        "group_name":"Moderators",
        "members":{
            "user2":true
        }
      }
   }
 }

我试过了:

const ref = firebase.database().ref('groups');
            ref.orderByChild("user2").on("child_added", function (snapshot) {
                console.log(snapshot.key)
            });

但没有运气

1 个答案:

答案 0 :(得分:1)

如果您要列出第2组的成员:

const ref = firebase.database().ref('groups');
ref.child("group2/members").on("child_added", function (snapshot) {
    console.log(snapshot.key)
});

这将打印:

  

用户2