User profile information is not showing up using swift and firestore database

风格不统一 提交于 2020-03-25 17:57:49

问题


pic of database I am trying to show a label with the user's status on their profile page. after logging in the user gets presented with a VC that has a side menu. on that side menu is a "profile" option. once choosing this they go to their profile controller. right now i simply need to search users/current uid/ "MembershipStatus" and present this result into a label called "welcomeLabel".

I am returning nul

import UIKit

import Firebase

class 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
        }
    }

回答1:


You are using Cloud Firestore for data storage but your code is reading data form RealTime Database. You have to read data like this:

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


来源:https://stackoverflow.com/questions/60728849/user-profile-information-is-not-showing-up-using-swift-and-firestore-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!