0

This is a copy of the question here: Querying in Firebase by child of child

There are two answers which are identical but neither of them actually work. I would simply respond to the answers but I don't have the reputation to do that - so I need to ask another question (stackOverflow failure - I can flood the website with multiples but I can't tell someone their answer doesn't work!?!)

I am working on a Firebase app that uses groups. Each group is structured like this

    groups : {
  "-KW5C5pS_OsWqS8_SLDy" : {
    "key" : "-KW5C5pS_OsWqS8_SLDy",
    "name" : "Test",
    "users" : {
      "uFpUrVFafVgVFsQudTNpOquIJZE2" : true
    }
  }
}

I would like to be able to query for groups that have the current users uid with either true or false indicating if they have admin rights or not. Right now my query looks like this:

FIRDatabase.database().reference()
        .child("groups").queryOrdered(byChild: "users/" + uid).queryEqual(toValue: writable)

This is identical to what was posted as an answer to the other question and stated over and over that it works - however it returns nothing!

Is this a bug or am I doing something wrong?

Community
  • 1
  • 1
Jim Hessin
  • 137
  • 2
  • 11

2 Answers2

0

I think that your query is right, you forgot the observer:

    FIRDatabase.database().reference()
    .child("groups").queryOrdered(byChild: "users/" + uid).queryEqual(toValue: writable).observe(FIRDataEventType.value, with: { (snapshot) in
 // Do what you need with your snapshot here
 // check if the snapshot is different from nil
})
Raul Marques
  • 355
  • 2
  • 9
  • I am passing this to a FUIArray - which does the observing for me. I have no issue when using a single child such as: query: FIRDatabase.database().reference().child("groupdata").child(group.key).child("items").queryOrdered(byChild: "parentlist").queryEqual(toValue: list.key) - It is only when using the "users/" + uid that I have issues – Jim Hessin Nov 09 '16 at 03:53
  • I have also tried changing the string to the equivalent "users/\(uid)" - with the same results. – Jim Hessin Nov 09 '16 at 03:59
0

Okay - I found my problem. I had set a rule to allow me to read only those groups that I am a member of - however evidently this isn't possible with the current Firebase rules language because you need to be able to read the entire directory in order to query it. I have changed the rules to allow all authenticated users to read all the groups and that solved this issue - though it isn't the ideal security.

It took me so long to figure this out because it wasn't giving me a permission denied error in the log - it just gave me an empty query.

Jim Hessin
  • 137
  • 2
  • 11
  • You can use the `withCancelBlock` variant of the `observe` method if you want to handle the permission denied error in your code. It's probably a good idea anyway for future queries. – vzsg Nov 11 '16 at 14:32