简体   繁体   English

如何使用 uid 文档 ID FirebaseAuth Firestore 获取 Firestore 字段信息

[英]How to get Firestore field information using an uid document id FirebaseAuth Firestore

In my code, when the user signs up with email, pass and firstName, I get in firebase: their email related to an uid and, in Cloud Firestore, I get the firstName with uid as document id inside "Users" collection.在我的代码中,当用户使用电子邮件、pass 和 firstName 进行注册时,我会进入 firebase:他们的电子邮件与 uid 相关,并且在 Cloud Firestore 中,我在“用户”集合中获取带有 uid 作为文档 ID 的 firstName。 I want to display user's name.我想显示用户名。 How to do it?怎么做?

EDIT:编辑:

In ModelData class, I have this code to signUp and added what was said below:在 ModelData 类中,我有这个代码来注册并添加以下内容:

func signUp(){
      
      // checking....
      
      if email_SignUp == "" || password_SignUp == "" || reEnterPassword == "" || userName == ""{
          
          self.alertMsg = "Error"
          self.alert.toggle()
          return
      }
      
      if password_SignUp != reEnterPassword{
          
          self.alertMsg = "Error"
          self.alert.toggle()
          return
      }
      
      withAnimation{
          
          self.isLoading.toggle()
      }
      
    Auth.auth().createUser(withEmail: email_SignUp, password: password_SignUp) { [self]    (result, err) in
          
          withAnimation{
              
              self.isLoading.toggle()
          }
          
          if err != nil{
              self.alertMsg = "Error"
              self.alert.toggle()
              return
          }
         let db = Firestore.firestore()
        let docRef = db.collection("PROFILE").document(user?.uid ?? "")
        
        db.collection("PROFILE").document(result!.user.uid).setData(["userName" : self.userName])
      
        

        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                let data = document.data()
                return data.userName
            } else {
                // Handle error here
            }
        }
        
        
          result?.user.sendEmailVerification(completion: { (err) in
              
              if err != nil{
                  self.alertMsg = err!.localizedDescription
                  self.alert.toggle()
                  return
              }
              
              // Alerting User To Verify Email...
              
              self.alertMsg = ""
              self.alert.toggle()
          })
      }
  }

and in userName in getting the error Value of type '[String : Any]?' has no member 'userName'并在userName中获取Value of type '[String : Any]?' has no member 'userName'的错误Value of type '[String : Any]?' has no member 'userName' Value of type '[String : Any]?' has no member 'userName'

You would query your Users firestore collection for the document id (the uid), get() the data, then return the data.firstName:您将查询您的 Users firestore 集合以获取文档 ID(uid),获取()数据,然后返回 data.firstName:

let docRef = db.collection("Users").document(uid)

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let data = document.data()
        return data.firstName
    } else {
        // Handle error here
    }
}

SOURCE 来源

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

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