微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Firestore获取数据以获取用户配置文件swift,iOS,Xcode,firebase / firestore

如何解决从Firestore获取数据以获取用户配置文件swift,iOS,Xcode,firebase / firestore

在这里有点新意,所以请客气。我是一名前空军飞行员,目前在法学院学习,所以编码不是我的专职工作...但是我正在尝试边走边学(以及帮助我​​的孩子们学习)。

我正在为我的iOS应用配置文件页面。我已经仔细阅读了Firebase文档,但是并没有详细说明我要在此处进行的操作。我也在这站点搜索以寻找答案...我发现了一些确实有用的东西,但是我觉得有些不对劲。我以前发布了此信息,但由于未收到任何有用的输入而将其删除

要做的是通过标签显示用户的数据(名字,姓氏,电话,地址等)。该代码(下面提供)可显示用户ID和电子邮件...我认为这是因为它是从身份验证中提取的,而不是从“用户”集合中提取的。该代码试图从用户集合中的相应文档中提取用户的其余数据。

这是viewController的完整代码。我已经尝试了很多次,但失败了,以至于我真的在最后一根稻草上。请帮忙!

我的猜测是,firstName变量不正确……是前面的数据库快照还是该变量的实际编码有问题。但是话又说回来...我不知道我在做什么...所以也许我对问题出在什么地方。

//  ClientDataViewController.swift

import UIKit
import Firebase
import FirebaseAuth
import FirebaseFirestore



class ClientDataViewController: UIViewController {

   
   
    
    
    @IBOutlet weak var firstNameLabel: UILabel!
    @IBOutlet weak var lastNameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var streetLabel: UILabel!
    @IBOutlet weak var street2Label: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var stateLabel: UILabel!
    @IBOutlet weak var zipLabel: UILabel!
    @IBOutlet weak var attorneyLabel: UILabel!
    @IBOutlet weak var updateButton: UIButton!
    @IBOutlet weak var passwordButton: UIButton!
    @IBOutlet weak var uidLabel: UILabel!
    
    
    
    
   let id = Auth.auth().currentUser!.uid
    let email = Auth.auth().currentUser!.email
    
   
  
   // MARK: Lifecycle
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.uidLabel.text = id
            self.emailLabel.text = email
            
        }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated) // call super
            
            getName { (name) in
                if let name = name {
                    self.firstNameLabel.text = name
                    print("great success")
                }
            }
        }
        
        // MARK: Methods
        
        func getName(completion: @escaping (_ name: String?) -> Void) {
            let uid = "dL27eCBT70C4hURGqV7P"
            let docRef = Firestore.firestore().collection("users").document(uid)

            docRef.getDocument { (document,error) in
                if let document = document,document.exists {
                    let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                    print("Document data: \(dataDescription)")
                } else {
                    print("Document does not exist")
                }
                completion("put the first name data here after we figure out what's in the doc")
            }
        }
            }

解决方法

以下内容可以解决您的问题。但是,我建议不要将idemail声明为强制展开的实例属性。它们甚至不需要成为实例属性,更不用说强制展开了。在使用可选值(尤其是这些授权属性)之前,请务必安全地对其进行拆包,因为如果用户未登录或在您下方退出(例如,过期的令牌),则应用程序将在此处崩溃,就像飞机一样,总是要避免。

class ClientDataViewController: UIViewController {
    @IBOutlet weak var firstNameLabel: UILabel!
    @IBOutlet weak var lastNameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var streetLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var stateLabel: UILabel!
    @IBOutlet weak var zipLabel: UILabel!
    @IBOutlet weak var attorneyLabel: UILabel!
    @IBOutlet weak var updateButton: UIButton!
    @IBOutlet weak var passwordButton: UIButton!
    @IBOutlet weak var uidLabel: UILabel!
    
    let id = Auth.auth().currentUser!.uid
    let email = Auth.auth().currentUser!.email
    
    // MARK: Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.uidLabel.text = id
        self.emailLabel.text = email
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated) // call super
        
        getName { (name) in
            if let name = name {
                self.firstNameLabel.text = name
                print("great success")
            }
        }
    }
    
    // MARK: Methods
    
    func getName(completion: @escaping (_ name: String?) -> Void) {
        guard let uid = Auth.auth().currentUser?.uid else { // safely unwrap the uid; avoid force unwrapping with !
            completion(nil) // user is not logged in; return nil
            return
        }
        Firestore.firestore().collection("users").document(uid).getDocument { (docSnapshot,error) in
            if let doc = docSnapshot {
                if let name = doc.get("firstName") as? String {
                    completion(name) // success; return name
                } else {
                    print("error getting field")
                    completion(nil) // error getting field; return nil
                }
            } else {
                if let error = error {
                    print(error)
                }
                completion(nil) // error getting document; return nil
            }
        }
    }
}

感谢您的服务!希望你能驾驶B1-B。

,

我从您问题的证据中怀疑您正在获取文档,但是检索到的文档中的字段名称不正确或字段未初始化。作为调试步骤,请用此函数替换dtruncnorm函数,该函数将打印文档中找到的所有数据。

getName

一旦我们了解了文档中的内容,就应该很容易算出要传递给完成函数的值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。