简体   繁体   中英

Firebase How to display a specific children of a child unknown id

I want to access the data of the children "name" and "photoURL" of the child "members"? You can see the structure of my database in images. The structure of my database is like that: 在此处输入图片说明

在此处输入图片说明

There's a map where users can show markers, these markers are events. I want to display in a tableView all members who are in the room except the user who created this room, it's the same who created the marker and put it on the map. I tried to use queryOrdered() and queryEqual() method because I know the room.key who is the value of the child "key" on the database. But after how can I access the child members and display in a tableView all members name with them them photo.

let refParticipants = refDatabase.child("markers").queryOrdered(byChild: "key").queryEqual(toValue: room.key)
refParticipants.observe(.childAdded, with: { snapshot in
    ...
}

I use Swift 3.1

Looking at your data structure it appears that you are saving key as a String (ex: "128330"). I don't know what room.key is, but if it's not a string I don't think .queryEqual(toValue: room.key) will work.

You can test this by running the following query:

ref.child("markers").queryOrderedByChild("key").queryEqual(toValue: "128330").observeSingleEvent(of: .value, with: {(snapshot) in
    print(snapshot) 
})

If the above query is not null then you've found the problem. If that doesn't work try changing .childAdded to .value .

ref.child("markers").queryOrderedByChild("key").queryEqual(toValue: room.key).observeSingleEvent(of: .value, with: {(snapshot) in
    print(snapshot) 
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM