[英]Kotlin Firestore - Fetching Fields from Specific Document
I have multiple collections which store various documents, and I'm attempting to reach a specific collection so that I may retrieve fields from a specific document.我有多个 collections 存储各种文档,我正在尝试访问特定集合,以便我可以从特定文档中检索字段。 I am attempting to do this in onStart
我正在尝试在onStart
中执行此操作
John Doe ----> "DocumentId (a23bsz12bvxaQw2)" ----> |name: John Doe, age: 23, occupation: teacher|
I am able to retrieve the specific collection in another method, however that's only because I'm able to pass in the name of the collection as a parameter.我能够用另一种方法检索特定集合,但这只是因为我能够将集合名称作为参数传递。 The only way for me to reach a specific Collection is by hard-coding the fieldPath
.我到达特定 Collection 的唯一方法是对fieldPath
进行硬编码。
This is my code thus far:到目前为止,这是我的代码:
override fun onStart() {
super.onStart()
val caseManagerNameOnStart = caseManagerName?.text
val notebookRef = db.collection("$caseManagerNameOnStart")
notebookRef.addSnapshotListener(this, object : EventListener<QuerySnapshot?> {
override fun onEvent(querySnap: QuerySnapshot?, ex: FirebaseFirestoreException?) {
if (ex != null) {
return
}
if (querySnap != null) {
for (documentSnapshot in querySnap) {
val appointment = documentSnapshot.toObject(
Appointment::class.java
)
appointment.setId(documentSnapshot.id)
appointment_text_view_date.text =
"Date: " + documentSnapshot["dateAppointment"].toString()
appointment_text_view_time.text =
"Time: " + documentSnapshot["timeAppointment"].toString()
appointment_text_view_case_manager.text =
"Case Manager: " + documentSnapshot["caseManagerName"].toString()
appointment_text_view_client_name.text =
"Client: " + documentSnapshot["userName"].toString()
appointment_text_view_format.text =
"Format: " + documentSnapshot["formatAppointment"].toString()
}
}
}
})
}
val caseManagerNameOnStart = caseManagerName?.text
seems to be empty, and the app crashes when I call the activity in question. val caseManagerNameOnStart = caseManagerName?.text
似乎是空的,当我调用相关活动时应用程序崩溃。 caseManagerName
should hold the name which can be used in for fieldPath
, however there is a case where there might not be a collection to retrieve, and thus nothing should be set to the TextViews. caseManagerName
应该包含可用于fieldPath
的名称,但是在某些情况下可能没有要检索的集合,因此不应为 TextView 设置任何内容。
My expected output is this:我预期的 output 是这样的:
onStart
from crashing app.如果没有可用的集合,请防止onStart
应用程序崩溃。Here is an image of my data in Firestore.这是我在 Firestore 中的数据图像。 Firestore Data Firestore 数据
I would highly appreciate any input.我将不胜感激任何意见。 Thank you for your time.感谢您的时间。
however that's only because I'm able to pass in the name of the collection as a parameter.但这只是因为我能够将集合名称作为参数传入。
It is mandatory to know the name of the collection, in order to access the properties of the documents.必须知道集合的名称,才能访问文档的属性。
val caseManagerNameOnStart = caseManagerName?.text seems to be empty, and the app crashes val caseManagerNameOnStart = caseManagerName?.text 似乎是空的,应用程序崩溃了
It is empty, the reason why you have that crash.它是空的,这就是您崩溃的原因。
caseManagerName should hold the name which can be used in for fieldPath caseManagerName 应该包含可用于 fieldPath 的名称
Or you can save the name of the collection in another type of object, for example, in SharedPreferences.或者你可以将集合的名称保存在另一种类型 object 中,例如在 SharedPreferences 中。
however there is a case where there might not be a collection to retrieve, and thus nothing should be set to the TextViews.然而,在某些情况下可能没有要检索的集合,因此不应为 TextView 设置任何内容。
This is how you can check a collection for existence, before setting something in the reference.这是在引用中设置某些内容之前检查集合是否存在的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.