根据子属性匹配获取密钥

时间:2017-04-07 16:44:00

标签: javascript firebase

因此,如果我在firebase中有一个结构如下的树,那么我该如何获取Name ==" Employee 1"这应该返回键" LJrWJlgUPWUaPQx0ok09CswIkXg2" ?

"Users" : {
    "LJrWJlgUPWUaPQx0ok09CswIkXg2" : {
      "Shifts" : {
        "dfaskjhfkashdf" : {
          "endTime" : 1490907600,
          "startTime" : 1490878800
        }
      },
      "email" : "Employee1@gmail.com",
      "employeeId" : "LJrWJlgUPWUaPQx0ok09CswIkXg2",
      "fcmToken" : "eOVPmlxy9fc:APA91bEChCT-JwnH14yLrkWBdWBR-KJwj_FznOgwSzu-2JbL2hD5tRTl-7GkavLSihSzJMJ2_f7FjDhUgnU464EKNGaTUjoV0ZuLbJMlxtJeghhRCBDTbkm_J_yiH29IDXHVjmxbfVHw",
      "name" : "Employee 1",
      "password" : "Employee1",
      "startDate" : "2017-01-23"
    }

1 个答案:

答案 0 :(得分:0)

如果我正确地理解了你的问题,那就应该这样做:

firebase.database().ref('Users')
  .orderByChild('name')
  .equalTo('Employee 1')
  .limitToFirst(1)
  .once('value', snap => {
    let key = Object.keys(snap.val())[0]; // LJrWJlgUPWUaPQx0ok09CswIkXg2
    let user = snap.child(key).val(); // the whole user object
  });