从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。

根据您问题中的证据,我怀疑您正在获取文档,但在检索到的文档中有一个不正确的字段名或一个未初始化的字段。作为调试步骤,用这个函数替换getName函数,它打印文档中的所有数据。

func getName(completion: @escaping (_ name: String?) -> Void) {
let uid = Auth.auth().currentUser!.uid
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")
}
}

一旦我们知道了文档中的内容,就可以很容易地计算出要传递给完成函数的值。

最新更新