【问题标题】:Need help solving Firebase query in Android Studio需要帮助解决 Android Studio 中的 Firebase 查询
【发布时间】:2021-07-17 09:55:55
【问题描述】:

如果弹出 Toast 消息让员工知道他不能接受更多服务,如果他有一个活跃的服务。如果不是,员工应该能够接受服务。

我的问题是 无论我做什么,这个 firebase 查询似乎都认为我的数据库中有不存在的文档。每次我去接受服务时,它都会显示吐司。这意味着有一个集合“服务”,其中文档的字段“serviceCompleted”等于“false”,但在我的数据库中,员工下没有集合“服务”

My database showing no collection "services" exist underneath "employees"

这是我的 Kotlin 代码

fun setButton(serviceID: String, eID: String){

        val btnAcceptService = view.findViewById<Button>(R.id.btnAcceptService)
        btnAcceptService.setOnClickListener {
            val queryEmpServices = database.collection("employees").document(eID).collection("services").whereEqualTo("serviceComplete", false)

            queryEmpServices.get().addOnSuccessListener { documents ->

                if (documents != null){
                    Toast.makeText(applicationContext,"You already have a service active!", Toast.LENGTH_SHORT).show()

                }else {

                    database.collection("services").document(serviceID).update("saccept", true).addOnSuccessListener {

                        database.collection("services").document(serviceID).get().addOnSuccessListener { document ->

                            if (document != null) {

                                val Location = document.get("ulocation").toString()
                                val serviceType = document.get("serviceType").toString()
                                val uComment = document.get("ucomment").toString()
                                val uID = document.get("uid").toString()

                                if (document.getBoolean("saccept") == true) {

                                    database.collection("users").document(document.get("uid").toString()).collection("services").document(serviceID).update("saccept", true).addOnSuccessListener {

                                        database.collection("employees").document(mAuth.currentUser!!.uid).get().addOnSuccessListener { document ->

                                            if (document != null) {

                                                val calendar = Calendar.getInstance()
                                                val simpleDateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
                                                val acceptDate = simpleDateFormat.format(calendar.time)

                                                val eFullName = document.get("ename").toString() + " " + document.get("esurname").toString()
                                                val eCompany = document.get("ecompany").toString()

                                                database.collection("users").document(uID).get().addOnSuccessListener { document ->


                                                    val uName = document.get("name").toString()
                                                    val uPhonenumber = document.get("phonenumber").toString()
                                                    val serviceAccept = EmployeeServiceAccept(acceptDate, serviceID, Location, serviceType, uComment, uName, uPhonenumber, false)

                                                    database.collection("employees").document(mAuth.currentUser!!.uid).collection("acceptedservices").document(serviceID).set(serviceAccept)
                                                    database.collection("services").document(serviceID).update("acceptedby", eFullName + ", " + eCompany)
                                                    database.collection("users").document(uID).collection("services").document(serviceID).update("acceptedby", eFullName + ", " + eCompany)
                                                    Toast.makeText(applicationContext, "Service Accepted", Toast.LENGTH_SHORT).show()



                                                }


                                            }


                                        }


                                    }


                                }

                            } else {

                                Toast.makeText(applicationContext, "Failed to accept service", Toast.LENGTH_SHORT).show()

                            }

【问题讨论】:

  • 但在我的数据库中,员工下没有集合“服务”,但它作为顶级集合存在,对吧?这有什么问题?
  • @AlexMamo 没有错。该集合用于存储所有服务数据(serviceID、serviceCat 等),但在集合“员工”中没有集合“服务”,但是当我查询该集合下的文档时,它返回的文档不为空(表示它存在)。稍后将在集合“员工”,子集合“服务”下有数据,但截至目前那里没有数据,我的查询仍然将文档返回为非空
  • 很抱歉,我在“fo3 ... Qm2”文档下看不到任何子集合。可以给我们看看吗?
  • @AlexMamo 这就是问题所在......没有集合,但我的查询返回有一个。我在帖子中粘贴的代码检查该集合下是否有任何“serviceAccepted”= false(“当前不存在”),如果没有,员工可以接受服务。
  • 所以你说Toast.makeText(applicationContext,"You already have a service active!", Toast.LENGTH_SHORT).show()被显示了,对吧?

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:

当您在Query 对象上使用Task.addOnSuccessListener(OnSuccessListener) 方法时,您得到的结果可能是成功或失败,nevernever空值。它永远是一个,或者另一个。

话虽如此,您永远不应该检查 documents 对象是否为 null,因为它永远可能为 null。相反,您应该做的是检查类型为 QuerySnapshot 的“文档”对象,以查看是否为 isEmpty()

if (!documents.isEmpty){
    Toast.makeText(applicationContext,"You already have a service active!", Toast.LENGTH_SHORT).show()
} else {
    Toast.makeText(applicationContext,"You don't have a service active!", Toast.LENGTH_SHORT).show()
}

确实会显示来自语句“else”部分的 Toast 消息,因为 QuerySnapshot 对象中不存在任何文档:

"You don't have a service active!"

【讨论】:

  • 解决了我的问题!工作出色,谢谢 Alex。
猜你喜欢
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多