0

I'm trying to get the key of a node based on the child. Here's my structure

Code:

RESTAURANTS
    KTNfWKLD0isCsrpys
        name: “McDonalds”
        loc: “LA”
    KTNfWILD0iIkLLekc
        name: “KFC”
        loc: “LV”

Code:

FIRDatabase.database().reference().child("RESTAURANTS").queryOrderedByChild("name").queryEqualToValue("McDonalds").observeEventType(.Value, withBlock: { (snapshot) in
    print(snapshot.key)
}, withCancelBlock: nil)  

How can I get the key of "McDonalds" in a query?

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
LEVIS OGCPAS
  • 229
  • 4
  • 11

1 Answers1

3

You have to use allKeys to get keys from snapshots and than loop it to get key one by one. Try this code

FIRDatabase.database().reference().child("RESTAURANTS").queryOrderedByChild("name").queryEqualToValue("McDonalds").observeEventType(.Value, withBlock: { (snapshot) in
   if snapshot.exists() {
      for key in (snapshot.value?.allKeys)!{
          print(key)
      }
   } 
}

As @Frank van Puffelen said, This way of iterating loses the ordering of the child nodes. If you want to get ordered list than use snapshot.children first

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • Note that this way of iterating loses the ordering of the child nodes. While that likely doesn't matter in this specific case (since OP uses an equality check), I find it in general best to stick with iterating over `snapshot.children` first and only then getting the `.value` of each child. – Frank van Puffelen Oct 07 '16 at 07:49
  • @FrankvanPuffelen Hi! I've been reading many posts on ordering based on descending with Firebase and I do need to keep the order, but am having trouble getting the key or value from a child when I iterate over children. In iOS it says child (NSFastEnumerationIterator) has no member key or value. It returns under each child the data that I have as a dictionary, but I need the key for that child. Thanks in advance! I have found your other comments in other posts helpful as well. – Renee Olson May 17 '17 at 00:56
  • @FrankvanPuffelen This is just my test data right now to understand how it works. But using the observeSingleEvent with .value, which works best for what I need. Below is a print statement of the entire child when I iterate. I cannot seem to find any way to get the key called " today" for this child. I can get the snapshot.key, but that is not what I need. Do you have any thoughts? ` child is Snap (today) { status = answered; time = "-1494972374.35696"; }` – Renee Olson May 17 '17 at 01:23
  • @FrankvanPuffelen Sorry for all the comments, but while searching I found that you answered my question, but currently it appears to not work (child.key) in iOS. Here's a link to your answer in 2016 - http://stackoverflow.com/questions/41307826/searching-through-child-values-firebase-swift – Renee Olson May 17 '17 at 01:33
  • @FrankvanPuffelen I was able to get help from firebase support. Turns out now you have to do - for child in snapshot.children.allObjects as! [FIRDataSnapshot] { print(child.key) } – Renee Olson May 18 '17 at 04:14