【问题标题】:User profile information is not showing up using swift and firestore database使用 swift 和 firestore 数据库未显示用户个人资料信息
【发布时间】:2020-03-17 19:13:49
【问题描述】:

pic of database 我正在尝试在他们的个人资料页面上显示带有用户状态的标签。登录后,用户会看到一个带有侧边菜单的 VC。在那一侧的菜单上有一个“个人资料”选项。一旦选择了这个,他们就会去他们的配置文件控制器。现在我只需要搜索 users/current uid/ "MembershipStatus" 并将这个结果呈现到一个名为 "welcomeLabel" 的标签中。

我返回 nul

导入 UIKit

导入 Firebase

类 NonMemberProfileController: UIViewController {

    // MARK: - Properties

var welcomeLabel: UILabel = {
    let label = UILabel()
    label.textColor = .white
    label.font = UIFont.systemFont(ofSize: 28)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.alpha = 0
    return label
}()

    // MARK: - Init

    override func viewDidLoad()
    {
            super.viewDidLoad()
            authenticateUserAndConfigureView()
    }

func loadUserData()
{
        guard let uid = Auth.auth().currentUser?.uid else {return}
                                            //.child("MembershipStatus")
    Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value) {
        (snapshot) in
        if snapshot.hasChild("MembershipStatus"){
            print("true we have bingo")
        } else {
            print("no bueno")
            dump(snapshot)
        }

        guard let status = snapshot.value as? String else { return }
        self.welcomeLabel.text = "Welcome, \(status)"
        print("this is lkjfdskjklsfad" + status)
        UIView.animate(withDuration: 0.5, animations: {
            self.welcomeLabel.alpha = 1
        })
    }
}

func authenticateUserAndConfigureView(){
    if Auth.auth().currentUser == nil {
        DispatchQueue.main.async {
            let navController = UINavigationController(rootViewController: LoginViewController())
            navController.navigationBar.barStyle = .black
            self.present(navController, animated: true, completion: nil)
        }
    } else {
        configureNavigationBar()
        loadUserData()
    }
}

        // MARK: - Selectors

        @objc func handleDismiss() {
            dismiss(animated: true, completion: nil)
        }

        // MARK: - Helper Functions

        func configureNavigationBar() {
            view.backgroundColor = UIColor.lightGray
            navigationItem.title = "Profile"
            navigationController?.navigationBar.barTintColor = .darkGray
            navigationController?.navigationBar.barStyle = .black
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Home_2x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
            navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "baseline_settings_white_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
            view.addSubview(welcomeLabel)
            welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        }
    }

【问题讨论】:

  • 请仔细检查您的数据库是否具有该路径的值。如果是,则在StatusMembership 观察事件的完成块中打印出快照值,转储为dump(snapshot.value),以在控制台上获取响应以验证它是否确实具有值。
  • 还有一件事。我的用户 uid 正在打印,所以我正在阅读。完全被这件事难住了.. :-/
  • 你试过打印 snapshot.value 吗?
  • 我刚刚更新了代码以反映一个插件。我尝试了你建议的转储(snapshot.value),但仍然返回 null。
  • This is from nonMembermain vc rdHIwVi9kcSaHcmz1H7ejko8xQS2 Show profile no bueno - Snap (rdHIwVi9kcSaHcmz1H7ejko8xQS2) #0 - super: NSObject 来自上面列出的代码

标签: swift firebase firebase-authentication


【解决方案1】:

您正在使用 Cloud Firestore 进行数据存储,但您的代码正在从实时数据库读取数据。你必须像这样读取数据:

let userRef = Firestore.firestore().collection("users").document(uid) 
userRef.getDocument { (documentSnapshot, error) in guard 
    let document = documentSnapshot?.data() else { 
        print(error)
        return 
    } 
    print(document) 
}

【讨论】:

    猜你喜欢
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多