在我的应用程序中,用户可以注册,并且这样做,我还保存了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的use setData和您知道的ID。
Firestore无法随时知道您想要哪个文档,除非您提供一种查找文档的方式。
关于ios - Swift-从随机创建的文档ID(Firestore)中获取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239794/