简体   繁体   English

从 firebase 获取用户数据

[英]Fetching user data from firebase

I'm currently trying to display the name of the user that is currently logged in, but I can't seem to get it to display the name.我目前正在尝试显示当前登录的用户的名称,但我似乎无法让它显示名称。 Can anyone see what's wrong with the code?任何人都可以看到代码有什么问题吗?

The console was printing the first name of every user so I added in the uuid but now I'm getting a "Use of unresolved identifier 'querySnapshot'" error控制台正在打印每个用户的名字,所以我在 uuid 中添加了但现在我收到“使用未解析的标识符 'querySnapshot'”错误

func getData() {
        let uuid = UUID().uuidString
        guard let currentUser = Auth.auth().currentUser else { return }
        let dbUsers = Firestore.firestore().collection("members").document(uuid)
        dbUsers.addSnapshotListener { snapshot, error in
        if error != nil {
                 print(error ?? "Couldn't update text field TextUser according to database")
            } else {
                for document in (querySnapshot?.documents)! {
                    if let Name = document.data()["firstName"] as? String {
                        print(Name)
                        var post = NameData(firstname: "")
                        post.firstname = Name

                        self.userName.append(post)
                        self.nameLabel.text = self.name
                    }
                }
                
                print(self.userName)
            }
        }
      }

The error message is telling you that you're using a variable called querySnapshot , but you never defined it.错误消息告诉您您正在使用一个名为querySnapshot的变量,但您从未定义它。

I suspect you meant to say for document in (snapshot?.documents)!我怀疑您的意思是for document in (snapshot?.documents)! instead.反而。

However, your snapshot is not a QuerySnapshot object, because you're only querying for one document here.但是,您的snapshot不是 QuerySnapshot object,因为您在这里只查询一个文档。 So there will be no documents property, and you will have nothing to actually iterate.因此将没有文档属性,并且您将没有任何实际迭代的内容。 If you want to query only for a single document, you should use a pattern more like what you see in the documentation for reading a document .如果您只想查询单个文档,则应该使用更像您在文档中看到的模式来阅读文档

Your query has several mistakes.您的查询有几个错误。

  1. This is a query for Document and not a collection, so you will be able to retrieve only one document ie the one with the uuid you provided.这是对 Document 的查询,而不是集合,因此您将只能检索一个文档,即具有您提供的uuid的文档。
  2. The placeholder in start of closure for query snapShot is snapshot but in for loop , you used querysnapShot which is an unresolved Identifier as well as querySnapshot.documents doesn't exist in your case.查询 snapShot 关闭开始时的占位符是snapshot但在for loop中,您使用querysnapShot ,这是一个未解析的标识符,并且querySnapshot.documents在您的情况下不存在。

To retrieve all the data from all the documents, replace your code with following要从所有文档中检索所有数据,请将您的代码替换为以下内容

func getData() {
    let uuid = UUID().uuidString
    guard let currentUser = Auth.auth().currentUser else { return }
    let dbUsers = Firestore.firestore().collection("members")
    dbUsers.addSnapshotListener { snapshot, error in
    if error != nil {
             print(error ?? "Couldn't update text field TextUser according to database")
        } else {
            guard let Snapshot = snapshot else { return }
            for document in (Snapshot.documents) {
                if let Name = document.data()["firstName"] as? String {
                    print(Name)
                    var post = NameData(firstname: "")
                    post.firstname = Name

                    self.userName.append(post)
                    self.nameLabel.text = self.name
                }
            }
            
            print(self.userName)
        }
    }
  }

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

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