i created this structure like this below.
`func sendMessage(_ message: Message) {
let document = db.collection("messages")
.document(message.senderId)
.collection(message.receiverId)
.document()
let messageData = ["fromId": message.senderId,
"toId": message.receiverId, "content": message.content,
"timestamp": message.timestamp] as [String : Any]
document.setData(messageData) { error in
if let error = error {
print("ERROR SAVING MESSAGE INTO FIRESTORE (error.localizedDescription)")
return
}
}
let recipientMessageDocument = db.collection("messages")
.document(message.receiverId)
.collection(message.senderId)
.document()
recipientMessageDocument.setData(messageData) { error in
if let error = error {
print("ERROR SAVING MESSAGE INTO FIRESTORE (error.localizedDescription)")
return
}
}`
and successfully fetch the messages like this:
`func fetchMessages(_ fromId: String, _ toId: String) {
db.collection("messages")
.document(fromId)
.collection(toId)
.addSnapshotListener { querySnapshot, error in
if let error = error {
print("FAILED TO LISTEM FOR MESSAGES: (error.localizedDescription)")
return
}
querySnapshot?.documentChanges.forEach({ change in
if change.type == .added {
let data = change.document.data()
DispatchQueue.main.async {
let newMessage = ChatMessage(documentId: change.document.documentID, data: data)
self.messages.append(newMessage)
}
}
})
}`
but when i want to pull all the receiverId i circled below in the picture, i get “document does not exist” error. How can i get those receiverId s?
This is how i try to fetch the receiverIds. Actually this function is for getting userNames but at the beginning of the function i can’t get content of the subcollection.