Reputation: 8701
Let's say I have database nodes structured like below: Each user sets username of his/her preference, and his/her UID assigned by Firebase auth is logged under the username.
"uid_match":
{
"username_1" : {
"uid" : "firebase_uid_1"
},
"username_2" : {
"uid" : "firebase_uid_1"
}
I'm trying to write codes to extract the username by the firebase uid. It will look for a certain firebase uid and return the username, which has its own URL.
Below is the part of the code I wrote, but it returns '/uid_match', instead of '/username_1' or '/username_2'.
let ref=firebase.database.ref('/uid_match');
let user=firebase.auth().currentUser;
ref.orderByChild('uid').equalTo(user.uid).once('value', function(snapshot) {
window.location.href='/'+snapshot.key;
})
Question: What will be the correct query code to extract "username_1" or "username_2", instead of "uid_match"? Thanks a lot in advance.
Upvotes: 2
Views: 155
Reputation: 80914
You need to iterate inside the snapshot
, to retrieve the keys username_1
and _username_2
:
let ref=firebase.database.ref('/uid_match');
let user=firebase.auth().currentUser;
ref.orderByChild('uid').equalTo(user.uid).once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot){
window.location.href='/'+childSnapshot.key;
});
});
Upvotes: 1