简体   繁体   English

需要帮助解决 Android Studio 中的 Firebase 查询

[英]Need help solving Firebase query in Android Studio

I'm trying to see if this "employee" has a service where the serviceComplete field is false (in other words trying to see if this employee has an active service that is not complete) if a Toast message pops up letting the employee know he cannot accept more services has he has an active one.我想看看这个“员工”是否有一个 serviceComplete 字段为 false 的服务(换句话说,试图查看这个员工是否有一个不完整的活动服务)如果 Toast 消息弹出让员工知道他如果他有一个活跃的服务,就不能接受更多的服务。 If not the employee should be able to accept the service.如果不是,员工应该能够接受服务。

My problem is no matter what I do this firebase query seems to think there are documents in my DB that do not exist .我的问题是无论我做什么,这个 firebase 查询似乎都认为我的数据库中存在不存在的文档 Every time I go to accept the service it displays the toast.每次我去接受服务时,它都会显示吐司。 Meaning there is a collection "services" where a document has the field "serviceCompleted" which is equal to "false" but in my DB there is no collection "services" under employees这意味着有一个集合“服务”,其中文档的字段“serviceCompleted”等于“false”,但在我的数据库中,员工下没有集合“服务”

My database showing no collection "services" exist underneath "employees"我的数据库显示“员工”下不存在集合“服务”

and here is my Kotlin code这是我的 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()

                            }

When you are using the Task.addOnSuccessListener(OnSuccessListener) method on a Query object, the result that you get can be a success or a failure, never both and never null.当您在Query对象上使用Task.addOnSuccessListener(OnSuccessListener)方法时,您获得的结果可能是成功或失败,永远不会同时出现,也不会为空。 It will always be one, or the other.它永远是一个,或另一个。

That being said, you should never check the documents object against nullity because it can never be null.话虽如此,您永远不应该检查documents对象是否为空,因为它永远不会为空。 What you should do instead, is to check the "documents" object, which is of type QuerySnapshot to see if isEmpty() or not:相反,您应该做的是检查“文档”对象,该对象属于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()
}

Where indeed the Toast message from the "else" part of the statement will be displayed, as there are no documents present in the QuerySnapshot object:来自语句“else”部分的 Toast 消息确实将显示在哪里,因为 QuerySnapshot 对象中不存在文档:

"You don't have a service active!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM