Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未使用swift和firestore数据库显示用户配置文件信息_Swift_Firebase_Firebase Authentication - Fatal编程技术网

未使用swift和firestore数据库显示用户配置文件信息

未使用swift和firestore数据库显示用户配置文件信息,swift,firebase,firebase-authentication,Swift,Firebase,Firebase Authentication,我试图在他们的个人资料页面上显示一个带有用户状态的标签。登录后,用户将看到一个带有侧菜单的VC。在那一侧的菜单上有一个“配置文件”选项。选择此选项后,他们将转到其配置文件控制器。现在我只需要搜索users/current uid/“MembershipStatus”,并将此结果显示在一个名为“welcomeLabel”的标签中 我马上回来 导入UIKit 进口火基 类非成员ProfileController:UIViewController{ // MARK: - Properties

我试图在他们的个人资料页面上显示一个带有用户状态的标签。登录后,用户将看到一个带有侧菜单的VC。在那一侧的菜单上有一个“配置文件”选项。选择此选项后,他们将转到其配置文件控制器。现在我只需要搜索users/current uid/“MembershipStatus”,并将此结果显示在一个名为“welcomeLabel”的标签中

我马上回来

导入UIKit

进口火基

类非成员ProfileController: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
        }
    }

您正在使用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) 
}

请仔细检查您的数据库是否具有该路径上的值。如果是,则在
StatusMembership
observing event的完成块中,使用dump as
dump(snapshot.value)打印快照值
在console上获取响应以验证它是否真的有值。还有一件事。我的用户uid正在打印,所以我正在阅读它。好的。完全被这个问题难住了:-/您是否尝试打印snapshot.value?我刚刚更新了代码以反映外接程序。我尝试了转储(snapshot.value)您建议了,但仍然返回null。这是来自非成员vc rdHIwVi9kcSaHcmz1H7ejko8xQS2显示配置文件编号bueno-Snap(rdHIwVi9kcSaHcmz1H7ejko8xQS2)#0-super:NSObject上面列出的代码的结果