[英]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.