Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  13] [ 6]  / answers: 1 / hits: 98146  / 8 Years ago, mon, november 7, 2016, 12:00:00

I have the following structure on my Firebase database:
firebase


I would like to search for a user by name, last name or email but as I don't have the user key in the level above I don't know how I can achieve this. I'm doing and administrator session so it wouldn't have access to the user key.


I have tried:


let usersRef = firebase.database().ref('users');
usersRef.orderByValue().on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});

But it brings all the users on the database. Any ideas?


More From » firebase

 Answers
107

You can use equalTo() to find any child by value. In your case by name:



ref.child('users').orderByChild('name').equalTo('John Doe').on(value, function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});


The purpose of orderByChild() is to define the field you want to filter/search for. equalTo() can get an string, int and boolean value.



Also can be used with auto generated keys (pushKey) too.



You can find all the documentation here


[#60153] Friday, November 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;