删除项目时遇到问题。
删除主题时,我要删除与主题具有相同ID的任务
我尝试过
mFirestore.collection("classes").document(classID).collection("subjects").document(subjectID).delete().addOnCompleteListener {
mFirestore.collection("classes").document(classID).collection("tasks").whereEqualTo("subject", subjectID).get().addOnCompleteListener{ task ->
if (task.isSuccessful) {
for (document in task.result)
mFirestore.collection("classes").document(classID).collection("tasks").document(document.id).delete()
}
}
}
但没有成功。
现在,我想使用Firebase函数执行此操作。我怎样才能做到这一点? Java或Kotlin中的代码相同
答案 0 :(得分:2)
尝试一下:-
mFirestore.collection("classes").document(classID).collection("subjects").document(subjectID)
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mFirestore.collection("classes").document(classID).collection("tasks").whereEqualTo("subject", subjectID).get().addOnCompleteListener(this, new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful())
{
for (QueryDocumentSnapshot documentSnapshot : task.getResult())
{
mFirestore.collection("classes").document(classID).collection("tasks").document(documentSnapshot.getId()).delete();
}
}
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error deleting document", e);
}
});