简体   繁体   English

如何获取 Firestore 文档中的密钥?

[英]How to get the keys in a Firestore document?

在此处输入图像描述

Hello, this is Kotlin Beginner.你好,我是Kotlin初学者。

While playing with Firestore, I suddenly had a question.在玩Firestore的时候,突然有个疑问。

The value of a field can be easily retrieved, but可以很容易地检索字段的值,但是

Is there a way to get the text of the field itself?有没有办法获取字段本身的文本?

I would like to take the blue square in the following image literally.我想从字面上理解下图中的蓝色方块。

Any help would be appreciated.任何帮助,将不胜感激。

DocumentSnapshot#getData() method, returns an object of type Map<String,. DocumentSnapshot#getData()方法,返回 Map<String, 类型的 object。 Any,>, To get the keys of a document: simply iterate through the Map object, as explained in the following lines of code: Any,>, 要获取文档的键:只需遍历 Map 对象,如以下代码行所述:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseFirestore.getInstance()
val uidRef = rootRef.collection("users").document(uid)
uidRef.get().addOnSuccessListener { document ->
    if (document != null) {
        document.data?.let { data ->
            data.forEach { (key, _) ->
                Log.d(TAG, key)
            }
        }
    } else {
        Log.d(TAG, "No such document")
    }
}.addOnFailureListener { exception ->
    Log.d(TAG, "get failed with ", exception)
}

To obtain the following result in the logcat:在logcat中得到如下结果:

email
id
nickname
password
phone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM