簡體   English   中英

iOS Firestore '文檔路徑不能為空'

[英]iOS Firestore 'Document path cannot be empty'

這不是問題,而是錯誤的解決方案:

*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Document path cannot be empty.'
terminating with uncaught exception of type NSException

有些人會遇到類似的問題,因為在舊版本的 Firebase 中,文檔的檢查語句只會檢查 nil 字符串而不是空字符串。 Firebase 的最新版本檢查 nil 和空字符串。

我的應用程序崩潰的原因:

如果用戶未登錄,我會啟動登錄/注冊視圖,如果他們已登錄,則應用程序會啟動主頁視圖,這是很常見的東西。 問題是我創建了一個 currentUser?.Uid 實例作為文檔路徑,如果用戶未登錄,它將返回空,沒有用戶登錄意味着沒有 UID,這意味着沒有文檔路徑。

我的 Firestore 會轉到用戶 -> UID -> 用戶。

結論

如果您遇到此問題,請確保您沒有為應用程序中任何位置的文檔路徑創建 currentUser?.UID 實例,除非用戶已登錄。我希望此解決方案能找到需要它的人,祝您編碼愉快。

我有同樣的問題,我的應用程序會崩潰。

我的代碼是:

func doesUserExist(completion: @escaping (Bool) -> Void) {
    guard AuthService.shared.Auth.auth().currentUser != nil else { return }
    firestore.collection("users").document(auth.currentUser?.uid ?? "").getDocument { snapshot, error in
        if snapshot != nil && error == nil {
            completion(snapshot!.exists)
        } else { completion(false) }
    }
}

^這將返回空文檔路徑。 所以我將我的代碼更改為以下內容並解決了它:

func currentUserDoc() -> DocumentReference? {
    if AuthService.shared.Auth.auth().currentUser != nil {
        return firestore.collection("users").document(auth.currentUser?.uid ?? "")
    }
    return nil
}

func doesUserExist(completion: @escaping (Bool) -> Void) {
    guard AuthService.shared.Auth.auth().currentUser != nil else { return }
    currentUserDoc()?.getDocument { snapshot, error in
        if snapshot != nil && error == nil {
            completion(snapshot!.exists)
        } else { completion(false) }
    }
}

希望它可以幫助任何人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM