javascript - Get key based on a sub-attribute match -
so if have tree structured following in firebase how can fetch unique key name == "employee 1" , should return key "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" }
if understand question correctly, ought trick:
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(); // whole user object });
Comments
Post a Comment