简体   繁体   English

如何在 Firebase Firestore 上获取一个字段?

[英]How can I get one field on Firebase Firestore?

Hello friends I'm doing practice about Firestore and Firebase Auth and I wanted to read only one field but I can't do that.朋友们好,我正在练习 Firestore 和 Firebase Auth,我只想阅读一个字段,但我做不到。 It's probably easy but I can't find anything in Inte.net.这可能很简单,但我在 Inte.net 中找不到任何东西。 Is anyone can help me.有没有人可以帮助我。 Have a good Codes:)有一个好的代码:)


A piece of my code我的一段代码

auth = Firebase.auth

private val db = Firebase.firestore.collection("user")


private fun getPic(){
val pic = db.document(auth.currentUser?.email.toString()).get(..?.) ....?..
}

I wanted to read that URL我想读那个 URL

我想阅读那个网址

There is no way to fetch just one field from a Firebase document ( https://stackoverflow.com/a/48312562/1896015 ).无法从 Firebase 文档 ( https://stackoverflow.com/a/48312562/1896015 ) 中获取一个字段。

You need to fetch the whole document, which is done asynchronously and then handle the received response, which contains the whole document data.您需要获取整个文档,这是异步完成的,然后处理接收到的包含整个文档数据的响应。

You also fetch the document from the collection which in this case is user which makes the whole path user/{email} .您还从集合中获取文档,在本例中是user ,它使整个路径成为user/{email}

From your code example this would probably look like this:从您的代码示例中,这可能看起来像这样:

private fun getPic() {
 val docRef = db.collection("user").document(auth.currentUser?.email.toString())
 docRef.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                Log.d(TAG, "picUrl: ${document.data.picUrl}")
            } else {
                Log.d(TAG, "No such document")
            }
        }
        .addOnFailureListener { exception ->
            Log.d(TAG, "get failed with ", exception)
        }
}

In this case you only log the information, but I guess you would want to return the picUrl from the function. I suggest looking in to Kotlin asynchronous functions for different ways to handle this: https://kotlinlang.org/docs/async-programming.html#callbacks在这种情况下,你只记录信息,但我想你会想要从 function 返回 picUrl。我建议查看 Kotlin 异步函数以了解不同的处理方法: https://kotlinlang.org/docs/async-编程.html#callbacks

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

相关问题 如何从 Firebase Firestore 获取 ReactJs 的数据? - How Can I get data with ReactJs from Firebase Firestore? REACT JS FIREBASE FIRESTORE- 我如何更新文档中的字段? - REACT JS FIREBASE FIRESTORE- how can i update my field in a document? Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 如何从firestore查询中只获取一个字段,我正在获取所有字段 - How to get only one field from firestore query, i am getting all the fields 如何从 flutter 的 firestore 中的一个字段发送和接收两种类型的数据? - How can I send and receive two types of data from one single field in firestore for flutter? 我在数组中遇到问题我从 firebase (firestore) 获取数据,我想显示数据数组我该怎么做? - I face a problem in the array I get the data from firebase (firestore) and I want to show array of data how can i do? 从 Firestore 中的一个文档中获取一个字段 - Get one field from one document in Firestore 如何按顺序显示 Firebase Firestore 数据? - How can I display Firebase Firestore data in order? 我如何判断它是图像还是视频 - Firebase Storage/Firestore - How can I tell if it's an image or video - Firebase Storage/Firestore 如何使用帮助文档 ID 更新 Cloud firestore 文档字段? - How can i Update Cloud firestore document Field with the help DocumentID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM