首页 > 解决方案 > FireBase 按文档和集合进行多个查询

问题描述

我正在努力使用 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))
}

标签: androidfirebasekotlingoogle-cloud-firestore

解决方案


 **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)
            }
    
        }

推荐阅读