2

I created the structure below on firebase and I need to get the auto id underlined in red:

enter image description here

The code I created to query the values:

let query = reference.queryOrdered(byChild: "receiverId").queryEqual(toValue: "LKupL7KYiedpr6uEizdCapezJ6i2")
        //Start process
        query.observe(.value, with: { (snapshot) in
            guard snapshot.exists() else{
                print("Data doesn't exists")
                return
            }
            print(snapshot.key)
}

My "snapshot.value" results in:

Optional({
    "-KaRVjQgfFD00GXurK9m" =     {
        receiverId = LKupL7KYiedpr6uEizdCapezJ6i2;
        senderId = bS6JPkEDXIVrlYtSeQQgOjCNTii1;
        timestamp = 1484389589738;
    };
})

How do I get only the string -KaRVjQgfFD00GXurK9m from the node above?

I had tried using "print(snapshot.key)" but it results on the user ID: bS6JPkEDXIVrlYtSeQQgOjCNTii1

Some ideas? Thank you very much.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Enzo
  • 73
  • 2
  • 9
  • When you post questions, please don't use images. Use text as it makes it searchable and we can copy and paste instead of having to re-type. Also, the senderId child node is a duplicate of the parent-parent node key (see my answer) and while there's nothing wrong with that, that same data can be retrieved from the parent snapshot (its essentially unneeded as you already have that data from the snapshot) – Jay Jan 14 '17 at 14:46

1 Answers1

13

.value returns the parent node and all of the child nodes that satisfy the results of the query. In this case, the children would need to be iterated over (there could be more than 1 child that matches the query).

The snapshot.key is the parent node key (bS6JPkEDXIVrlYtSeQQgOjCNTii1) and the value is a snapshot (dictionary) of all of the children. Each of those children is a key:value pair of uid:value and value a dictionary of the child nodes, receiverId, senderId etc

This code will print the uid of each child node and one of the values, timestamp, for each user node that matches the query.

    let usersRef = ref.child("users")
    let queryRef = usersRef.queryOrdered(byChild: "receiverId")
                           .queryEqual(toValue: "LKupL7KYiedpr6uEizdCapezJ6i2")

    queryRef.observeSingleEvent(of: .value, with: { (snapshot) in

        for snap in snapshot.children {
            let userSnap = snap as! FIRDataSnapshot
            let uid = userSnap.key //the uid of each user
            let userDict = userSnap.value as! [String:AnyObject]
            let timestamp = userDict["timestamp"] as! String
            print("key = \(uid) and timestamp = \(timestamp)")
        } 
    })
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Jay, It worked! Thank you very much and sorry the image, i will avoid it next time! To complete the answer, I just modified the line "**let timestamp = userDict["timestamp"] as! String**" to "**let timestamp = (userDict["timestamp"]?.stringValue)! as String**" to avoid the error: "Could not cast value of type 'NSDecimalNumber' to 'NSString' ." – Enzo Jan 14 '17 at 17:27
  • @Jay. Great !! Your answer helped me find out where I was mistaking and got me to understand snapshots from Firebase..finally. Many thanks. – Vincenzo Mar 02 '19 at 13:28