如何在Firebase中获取包含特定属性的节点?

时间:2017-12-25 13:18:03

标签: firebase firebase-realtime-database

例如 - 如何获取与email = -L1BchvA8NaQ9Yh7CjS_匹配的节点tomcruise@firebasemyapp.com?当数据库看起来像

{
  "-L1BchvA8NaQ9Yh7CjS_" : {
    "createdAt" : 1514187972592,
    "displayName" : "tom cruise",
    "email" : "tomcruise@firebasemyapp.com",
    "firstName" : "tom",
    "lastName" : "cruise",
    "profileLink" : "https://reactjs.org/logo-og.png",
    "status" : "happy christmas"
  },
  "-L1CcUEBOECrJJozlHJv" : {
    "createdAt" : 1514204689673,
    "displayName" : "Robert John Downey Jr",
    "email" : "robertjohndowneyjr@firebasemyapp.com",
    "firstName" : "Rober John",
    "lastName" : "Downey Jr",
    "profileLink" : "https://reactjs.org/logo-og.png",
    "status" : "happy christmas"
  },
}

编辑: -

我正在使用react-native.js

2 个答案:

答案 0 :(得分:2)

你没有提到你正在使用的语言,但我会用javascript回答,并且改成另一种语言应该不难。

myvar

答案 1 :(得分:1)

我从Common SQL Queries converted for the Firebase Database找到了解决方案。

var currentUserUID = firebase.auth().currentUser.uid;
let dbRef = firebase.database().ref("ARUserProfiles/" + currentUserUID);
dbRef
  .orderByChild("email")
  .equalTo("tomcruise@firebasemyapp.com")
  .once(
    "value",
    response => {
      const val = response.val();
      let responsePayload;
      if (val) {
        responsePayload = Object.entries(val);
        console.log("ARUserProfiles :-", responsePayload);
      }
    },
    error => {
      console.log(error);
    }
  );