简体   繁体   English

如何在 Firestore 中读取当前用户配置文件

[英]How to read the current users profile document in Firestore

I am working on my first IOS app using Firestore.我正在使用 Firestore 开发我的第一个 IOS 应用程序。 I am successfully retrieving all user documents or a specific document with a document name from the "users" collection.我成功地从“用户”集合中检索所有用户文档或具有文档名称的特定文档。 But as soon as I want to query the currentUsers documents I have trouble.但是,一旦我想查询 currentUsers 文档,我就遇到了麻烦。 I need the data for a user profile viewController.我需要用户配置文件 viewController 的数据。

I can query a specific document and print it with this code:我可以查询特定文档并使用以下代码打印它:

func loadUserData() {
    let userDocumentRef = Firestore.firestore().collection("users").document(
        "6tIzsXgbOMDIFpdgR18i")
    
    userDocumentRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let userData = document.data().map(String.init(describing: )) ?? "nil"
            print("Document data: \(userData)")
        } else {
            print("document does not exist")
            
        }
    }
}

But as soon as I try to get the currentUsers document with this code, then I get the error that "document does not exist":但是,一旦我尝试使用此代码获取 currentUsers 文档,就会收到“文档不存在”的错误消息:

func currentUserData() {
    let userID : String = (Auth.auth().currentUser?.uid)!
    let userRef = Firestore.firestore().collection("users").document(userID)
    userRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let userData = document.data().map(String.init(describing: )) ?? "nil"
            print("Document data: \(userData)")
        } else {
            print("document does not exist")
            
        }
    }
}

Here is a screenshot of the users collection in Firestore.这是 Firestore 中用户集合的屏幕截图。 Screenshot of Firestore users collection Firestore 用户集合的屏幕截图

I just simply cannot figure out what I am doing wrong, so any help is appreciated.我只是无法弄清楚我做错了什么,所以任何帮助表示赞赏。

It seems like there is an issue when mapping the document.映射文档时似乎存在问题。 Here is how you can map data from Firestore to Swift:以下是如何将 map 数据从 Firestore 传输到 Swift:

struct UserProfile: Codable, Identifiable {
  @DocumentID var id: String?
  var userId: String
  var firstName: String
  // ... other attributes
}

func currentUserData() {
  // Note: this is unsafe, please use an auth state change listener instead
  // Also, please try to avoid using force unwrap - your app will crash if the attribute is nil
  let userID : String = (Auth.auth().currentUser?.uid)!

  let userRef = Firestore.firestore().collection("users").document(userID)
  userRef.getDocument { (document, error) in
    if let document = document, document.exists {
      let userData = document.data(as: UserProfile.self)
      print("Document data: \(userData)")
    } else {
      print("document does not exist")
    }
  }
}

Here is how to set up an auth state change listener: https://firebase.google.com/docs/auth/ios/start#listen_for_authentication_state以下是如何设置身份验证 state 更改侦听器: https://firebase.google.com/docs/auth/ios/start#listen_for_authentication_state

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM