The following code tries to extract the text message from Firebase database data structure seen above, but returns a snapshot.value of nil instead and doesn't work. Additionally, when I run the "hasChildren" function, it returns false. The chatListingID is correctly populated with the chatListing's (not Messages) childByAutoID value.
lazy var chatRef: FIRDatabaseReference = FIRDatabase.database().reference().child("chatListings")
func configureChatListingCell(chatListing: ChatListing){
chatListingID = chatListing.chatRoomKey
chatRef.child(chatListingID).child("messages").child("MediaType").queryEqual(toValue: "TEXT").queryLimited(toLast: 1).observe(.value, with:{
snapshot in
print("snapshot.value is \(snapshot.value)")
for child in snapshot.children {
let childData = child as! FIRDataSnapshot
print("childData is \(childData)")
if let dict = childData.value as? NSDictionary{
lastMessage = dict["text"] as! String
lastMessagerId = dict["senderId"] as! String
}
print("Most recent message is \(lastMessage) from user \(lastMessagerId).")
}
})
Theoretically, I should be returning one result that has the most recent text message and senderId. Those two values are what I'm interested in.