我正在嘗試使用以下代碼獲取我的用戶檔案的子集合
func readData(){
let userId = Auth.auth().currentUser?.uid
self.db.collection("users/\(userId)/saved").getDocuments { (snapshot, err) in
if let err = err {
print("err")
}
if let userId != nil {
for document in snapshot!.documents {
let docId = document.documentID
let cty = document.get("city") as! String
let ccode = document.get("code") as! String
let countr = document.get("country") as! String
print(cty, ccode, countr,docId)
}
}
}
但是我的代碼沒有列印任何東西,我不明白這個問題,檔案存在,見下圖

uj5u.com熱心網友回復:
您userId在快照回傳中使用非法語法進行檢查,但邏輯流程是更大的問題。我建議您在獲取子集合之前檢查用戶是否已登錄,并檢查是否存在可行的快照,而不是檢查身份驗證狀態。
func readData() {
guard let userId = Auth.auth().currentUser?.uid else {
return
}
db.collection("users/\(userId)/saved").getDocuments { (snapshot, error) in
guard let snapshot = snapshot else {
if let error = error {
print(error)
}
return
}
for doc in snapshot.documents {
guard let city = doc.get("city") as? String,
let code = doc.get("code") as? String,
let country = doc.get("country") as? String else {
continue // continue document loop
}
let docId = doc.documentID
print(city, code, country, docId)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/431350.html
