简体   繁体   English

Kotlin Firestore - 从特定文档中获取字段

[英]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 是这样的:

  1. Get specified collection获取指定集合
  2. Retrieve fields from document从文档中检索字段
  3. Display these fields in given TextViews在给定的 TextView 中显示这些字段
  4. If no collection is available, prevent 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.

相关问题 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore 如何在已经从 Firestore KOTLIN 获取 arrayList 的片段上实现 SearchView - How to implement SearchView on fragment that is already fetching arrayList from firestore KOTLIN 如何从 Firestore 检索文档并将其分配给变量? [安卓] [科特林] - How to retrieve a document from Firestore and assign it to a variable? [ANDROID] [KOTLIN] 如何从 flutter 中的 firestore 文档字段获取数据? - How to get data from firestore document fields in flutter? 在 Firestore 中更新文档时阻止用户添加不同的字段? - Preventing a user from adding a different fields when updating a document in firestore? Firestore 从集合中文档的 get() 中排除特定字段 - Firestore exclude specific fields from get() of documents in collection 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 如何从 Firestore 获取所有文档的特定字段? - How to get specific fields for all documents from Firestore? Query Document 在 Flutter 的 Firestore 中获取特定 collections 的快照 - Query Document to get snapshots from specific collections in Firestore in Flutter 如何在包含文档中对象的对象数组中搜索特定字段并在 firestore (firebase) (JS) 中更新它们 - How to search for specific fields in an array of objects containing objects in a document and update them in firestore (firebase) (JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM