抓住当前用户的名字云firebase swift [英] grab the current users first name cloud firebase swift

查看:159
本文介绍了抓住当前用户的名字云firebase swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储用户的Firebase身份验证,但它还在Cloud Firestore数据库中构建了一个用户集合.我可以获取用户的名字,但是我遇到的问题是,它始终是添加的最后一个用户.

I have a Firebase Auth that stores the users, but it also builds a collection of users in the Cloud Firestore database. I can grab the users first name, but the problem I have is that it is always the last user that was added.

这是我的功能迅速

func welcomeName() {

    let db = Firestore.firestore()
    if let userId = Auth.auth().currentUser?.uid {

    var userName = db.collection("users").getDocuments() { (snapshot, error) in
        if let error = error {
            print("Error getting documents: \(error)")
    } else {
        //do something
            for document in snapshot!.documents {
        var welcomeName = document["firstname"] as! String
        self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
                }
            }
        }
    }
}

在Firebase云中,我的用户照此存储

in firebase cloud my users are stored as so

开始收藏是用户"

添加文档为自动ID

那么集合是

名字 简"

姓氏 母鹿"

uid "IKEPa1lt1JX8gXxGkP4FAulmmZC2"

uid "IKEPa1lt1JX8gXxGkP4FAulmmZC2"

有什么想法吗?

推荐答案

在循环浏览所有用户文档的列表时,它将在标签上显示上一位用户的firstName.您可能要显示第一个用户,如下所示,

As you are looping through a list of all user documents so it will show last user's firstName on the label. You might want to show the first user as below,

if let firstUserDoc = snapshot?.documents.first {
   var welcomeName = firstUserDoc["firstname"] as! String
   self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
}

如果列表中的uiduserId相同,则对于当前用户可能是这样,

Or may be this for current user if uid in the list is same as userId,

if let currentUserDoc = snapshot?.documents.first(where: { ($0["uid"] as? String) == userId }) {
   var welcomeName = currentUserDoc["firstname"] as! String
   self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
}

这篇关于抓住当前用户的名字云firebase swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆