简体   繁体   English

iOS Firestore '文档路径不能为空'

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

This is not a question, but a solution to the error:这不是问题,而是错误的解决方案:

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

Some people would get a similar issue because in an older version of Firebase, the check statements for document would only check for a nil string rather than an empty.有些人会遇到类似的问题,因为在旧版本的 Firebase 中,文档的检查语句只会检查 nil 字符串而不是空字符串。 The latests versions of Firebase check for nil and an empty string. Firebase 的最新版本检查 nil 和空字符串。

The reason why my app would crash:我的应用程序崩溃的原因:

I would initiate a sign in/ sign up view if the user is not signed in, if they are signed in then the app initiates a homeview, pretty common stuff.如果用户未登录,我会启动登录/注册视图,如果他们已登录,则应用程序会启动主页视图,这是很常见的东西。 The issue was that I had created an instance of currentUser?.Uid as the document path, which would return empty if the user is not signed in, no user signed in means no UID which means no document path.问题是我创建了一个 currentUser?.Uid 实例作为文档路径,如果用户未登录,它将返回空,没有用户登录意味着没有 UID,这意味着没有文档路径。

My firestore would go Users -> UID -> User.我的 Firestore 会转到用户 -> UID -> 用户。

Conclusion结论

If you have this issue make sure you are not creating an instance of currentUser?.UID for a document path anywhere in your app unless the user is signed in. I hope this solution finds whoever needs it, happy coding.如果您遇到此问题,请确保您没有为应用程序中任何位置的文档路径创建 currentUser?.UID 实例,除非用户已登录。我希望此解决方案能找到需要它的人,祝您编码愉快。

I had same issue and my app would crash.我有同样的问题,我的应用程序会崩溃。

My code was:我的代码是:

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) }
    }
}

^This would return empty document path. ^这将返回空文档路径。 So I changed my code to the following and it solved it:所以我将我的代码更改为以下内容并解决了它:

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) }
    }
}

Hope it helps anyone.希望它可以帮助任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FIRInvalidArgumentException,原因:文档路径不能为空 - FIRInvalidArgumentException, reason: Document path cannot be empty Flutter firebase firestore:'path.isNotEmpty':文档路径必须是非空字符串) - Flutter firebase firestore: 'path.isNotEmpty': a document path must be a non-empty string) 如何检查 Firestore 中的文档是否为空? - How to check if a document is empty in Firestore? 抓紧修复 firebase“文档路径不能为空” - do catch to fix firebase "document path cannot be empty" Firestore 获取已查询文档的路径 - Firestore get path of already queried document 如何获取firebase firestore中完整的文档路径? - How to get the complete document path in firebase firestore? Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions Firestore 错误:文档必须具有偶数个路径元素 - Firestore error: A document must have an even number of path elements Cloud Firestore 文档添加给出错误“参数“数据”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值” - Cloud Firestore document add gives error "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value" 如何从 Firestore 中的空文档中删除嵌套的 collections? - How to delete nested collections from an empty document in Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM