在我的应用程序,用户可以注册并通过这样做,我也救了firstname
,lastname
和uid
。
这是Firestore-Database的样子:
我想访问用户,lastname
但是我不知道如何访问,因为Document-ID
获取是在注册过程中随机创建的。
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
//check for errors
if let err = err {
self.view.endEditing(true)
self.showError(err.localizedDescription)
}else {
//user was created successfully; store first and last name
let db = Firestore.firestore()
db.collection("users").addDocument(data: ["firstname":firstName, "lastname": lastName, "uid": result!.user.uid]) { (error) in
if error != nil {
self.showError("Error saving user data")
}
}
//transition to home
self.transitionToHome()
}
您需要了解两件事之一才能在Firestore中获取文档:
如果您都不知道这些事情中的任何一个,那么您所能做的就是获取集合中的所有文档,并通过查看集合中的所有内容来找出想要的内容。
最重要的是,如果您不知道哪个文档属于哪个用户,那您一定会感到困惑。您应该将用户的ID存储在文档中,或者作为文档本身的ID存储,以便以后可以使用它来查找该文档。
我建议使用Firebase Auth用户ID作为文档ID。这是非常普遍的。不要使用addDocument编写带有随机ID的文件,而应将setData与您知道的ID一起使用。
Firestore无法随时知道您想要哪个文档,除非您提供一种查找文档的方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句