无法从子集合中的现有文档中获取数据(Swift和Firestore)

时间:2019-04-04 15:13:18

标签: swift google-cloud-firestore

enter image description here

消息结构:

消息> currentUserID(文档)> partnerID(集合)>消息(文档)

我可以检索到合作伙伴ID,但无法检索集合中的单个消息(文档)。这是Im使用的功能:

func observeUserMessages(){
    guard let uid = Auth.auth().currentUser?.uid else { return }

    let dbRef = db.collection("messages").document(uid).addSnapshotListener { (querySnapshot, error) in
        guard let snapshot = querySnapshot?.documentID else { return }
        print("This is the partner ID: \(snapshot)")
        self.fetchMessageWithPartnerID(partnerID: snapshot)
    }
    self.tableView.reloadData()
}

fileprivate func fetchMessageWithPartnerID(partnerID: String) {
    guard let uid = Auth.auth().currentUser?.uid else { return }

    Firestore.firestore().collection("messages").document(uid).collection(partnerID).getDocuments { (snapshot, err) in
        print("This is the snapchat count:\(snapshot?.count)")
    }
}

结果:

enter image description here

如您所见,它应该显示两条消息,但不返回任何内容。

1 个答案:

答案 0 :(得分:0)

我认为.collection().document()之间是有区别的。试试

Firestore.firestore().collection("messages").collection(uid).collection(partnerID).getDocuments { (snapshot, err) in
    print("This is the snapchat count:\(snapshot?.count)")
}