Reputation: 253
I´m trying to get data from Firebase depending on the name and city from object. My firebase tree looks like this:
MYAPP
Object:
- 12837291ß2837(a random ID):
"name": "test"
"city": "Hong Kong"
- 12382133193u2:
"name": "test"
"city": "Paris"
- 2137829128738:
"name": "test2"
"city": "Frankfurt"
So for example i just want to get the Object where the name is "test" and the city is "Hong Kong".
i tried sth like this but i dont get any Data:
let ref = FIRDatabase.database().referenceFromURL("https://myRef")
ref.queryOrderedByChild("Object").queryEqualToValue("test").observeEventType(.ChildAdded) { (snapshot) in
print(snapshot)
}
I also added rules in Firebase like :
".indexOn": "Object"
Upvotes: 0
Views: 480
Reputation: 598728
Two main problems:
To sort/filter you first specify the property that you want to filter on and then the filtering operation. Since the value you specify is from the name
property, the correct query is:
let ref = FIRDatabase.database().referenceFromURL("https://myRef")
let query = ref.queryOrderedByChild("name").queryEqualToValue("test")
query.observeEventType(.ChildAdded) { (snapshot) in
print(snapshot)
}
Firebase Database only supports ordering/querying on a single properties. See Query based on multiple where clauses in firebase.
Upvotes: 1