0

I have an Android Studio project with 2 user collections: Trainers and Trainees. I want to make a method that receives the user's String email as input (the user's email is the document ID in both collections and a user can be either in the trainees collection or in the trainers collection not in both) and the method returns a boolean containing true if the user is a trainer and false if he isn't (meaning he is a trainee).

What is the best way of doing this?

I tried many things like this:

public static boolean isTrainer(String email)
{
    FirebaseFirestore firestore = FirebaseFirestore.getInstance();
    DocumentReference trainerRef = firestore.collection("trainers").document(email);
    DocumentReference traineeRef = firestore.collection("trainees").document(email);
    
    // Check if the trainer document exists
    Task\<DocumentSnapshot\> trainerTask = trainerRef.get();
    boolean isTrainer = trainerTask.isSuccessful() && trainerTask.getResult().exists();
    
    // Check if the trainee document exists
    Task\<DocumentSnapshot\> traineeTask = traineeRef.get();
    boolean isTrainee = traineeTask.isSuccessful() && traineeTask.getResult().exists();
    
    // If the trainer document exists but the trainee document doesn't, the user is a trainer
    Log.d("FirestoreExample", "isTrainer: " + isTrainer + ", isTrainee: " + isTrainee);
    
    return isTrainer && !isTrainee;
}

And other things but nothing worked.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Raz Dar
  • 13
  • 3
  • Please don't post duplicate [questions](https://stackoverflow.com/questions/75852253/how-to-check-if-a-document-exists-in-a-firestore-collection). I'll write you an answer right away to the old question. – Alex Mamo Mar 27 '23 at 06:02

0 Answers0