【发布时间】: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