[英]getting documents from a subcollection Firestore swiftUI
I'm trying to retrieve data from documents in a subCollection in Firestore, but it returns an empty array after executing it.我正在尝试从 Firestore 的子集合中的文档中检索数据,但它在执行后返回一个空数组。
I tried to do the following.我尝试执行以下操作。
@Published var OrdersID = [String]()
func fetchOrdersID(){
FirebaseManager.shared.firestore.collection("Users").document(id).collection("OrdersID").getDocuments { snapshot, error in
guard let documents = snapshot?.documents else {
print("No documents")
return
}
DispatchQueue.main.async {
self.OrdersID = documents.map{ d in
return d["id"] as? String ?? ""
}
}
}
}
but still it returns an empty array of OrdersID outside this function, and inside of it returns the proper array list(IMAGE 1 shows this, orders ID is the content of the OrdersID array IMAGE 1 )但它仍然在 function 之外返回一个空的 OrdersID 数组,并在其中返回正确的数组列表(IMAGE 1 显示了这一点,订单 ID 是 OrdersID 数组IMAGE 1的内容)
You could try escaping with a completion..您可以尝试 escaping 完成..
@Published var OrdersID = [String]()
func fetchOrdersID(withCompletion completion: @escaping ((_ orderID: String) -> (Void))) {
FirebaseManager.shared.firestore.collection("Users").document(id).collection("OrdersID").getDocuments { snapshot, error in
guard let documents = snapshot?.documents else {
print("No documents")
return
}
let orderID = documents["id"] as? String ?? ""
completion(orderID)
}
}
And then when you go to call, you can add to the OrdersId...然后当你go来电话时,你可以添加到OrdersId ...
fetchOrdersID { orderID in
OrdersID.append(orderID)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.