【问题标题】:FireBase multiple queries by document and collectionFireBase 按文档和集合进行多个查询
【发布时间】:2020-10-14 12:53:40
【问题描述】:

我正在努力使用 firebase 运行一个查询来获取 truckDocumentId,然后运行另一个查询来获取 routesByDateDocumentId,最后我使用两个文档 ID 来运行函数“sendGpsPosition”,我的问题是第一个查询找到 truckDocumentId 但有时第二个查询不执行,这就是应用程序停止的原因。以下代码适用于 Kotlin。

如果我在调试,那么大部分时间都可以工作。如果我关闭调试,它几乎会显示下面的错误 =>

因为查询没有执行,我得到了这个错误:java.lang.IllegalArgumentException: Invalid document reference。文档引用必须有偶数个段,但卡车有 1 个

suspend fun getTruckId() {
    val trucksReference = firestore.collection("trucks").whereEqualTo("dispatcher", "Miro")
        .whereEqualTo("name", "PEUGEOT").get().await()
    val document = trucksReference.documents[0]
    if (document != null) {
        truckDocumentId = document.id
    }
}

suspend fun getRouteReferenceId() {
    val routesByDate = firestore.collection("trucks")
        .document(truckDocumentId)
        .collection("routes_by_date").get().await()
    val documentRoute = routesByDate.documents[0]
    if (documentRoute != null) {
        routesByDateDocumentId = documentRoute.id
    }
}


fun sendGpsPosition(lat: Double, long: Double, imageRef: String? = null) {
    runBlocking { getTruckId() } // if I get this DocumentID
    runBlocking { getRouteReferenceId() } // this here maybe will be not found or maybe will be found.. the async is not done correct not sure how to do it.
    firestore
        .collection("trucks")
        .document(truckDocumentId)
        .collection("routes_by_date")
        .document(routesByDateDocumentId)
        .collection("live_route")
        .add(LatLong(Timestamp.now(), lat, long))
}

【问题讨论】:

  • 不是显示我们看不到其值的变量,而是硬编码所有文档和集合字符串并显示该代码。或者,向我们展示导致此错误的所有变量的值。我建议阅读:stackoverflow.com/help/minimal-reproducible-example
  • 问题是有时我在第一个查询中找到了documentId,但第二个没有找到它......它崩溃了......如果我调试它没问题,我需要做它“异步“不知何故与协程......但我无法让它工作我改变了代码但仍然无法工作
  • 现在查看,我更新了代码
  • 我们仍然看不到您在查询中使用的变量的值。您应该在使用它们之前记录它们以确保它们是您所期望的。
  • 默认值是: var routesByDateDocumentId = "" var truckDocumentId = "" 所以如果我没有从firebase得到任何东西,我会放“”,因为这会崩溃..不知何故进程不会等待1 个过程要完成,抱歉第一天使用 Android Kotlin,.... :D 我的老板告诉我要修复一些东西,它与我过去使用的不太相似

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:
 **I solved it this way.**

private suspend fun getTruckId() {
            val trucksReference = firestore.collection("trucks")
                .whereEqualTo("dispatcher", "Miro")
                .whereEqualTo("name", "VW")
                .get()
                .await()
            val document = trucksReference.documents[0]
            if (document != null) {
                truckDocumentId = document.id
            }
        }
    
        private suspend fun getRouteReferenceId() {
            val currentTime = Timestamp.now()
            val routesByDate = firestore.collection("trucks")
                .document(truckDocumentId)
                .collection("routes_by_date")
                .get()
                .await() // here will be better to look for data by delivery_day
            val documentRoute = routesByDate.documents[0]
            if (documentRoute != null) {
                routesByDateDocumentId = documentRoute.documents[0].id
            }
        }
    
        private fun addGpsDataInDatabase(lat: Double, long: Double, imageRef: String? = null) {
            firestore
                .collection("trucks")
                .document(truckDocumentId)
                .collection("routes_by_date")
                .document(routesByDateDocumentId)
                .collection("planned_route")  //planned_route or live_route depends if we want to show current state of a truck of make a route plan
                .add(LatLong(Timestamp.now(), lat, long))
        }
    
        fun sendGpsPosition(lat: Double, long: Double, imageRef: String? = null) {
            GlobalScope.launch {
                val truckDocId = async { getTruckId() }
                truckDocId.await()
                val routeDocId = async { getRouteReferenceId() }
                routeDocId.await()
                addGpsDataInDatabase(lat, long, imageRef)
            }
    
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2019-12-28
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2023-01-19
    相关资源
    最近更新 更多