[英]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.