0

I am doing a project for school - android app which registers users to realtime database after it checks if there's a corresponding card number and phone number in a different database in Firestore. At the moment it verifies only the first document, but it wouldn't find the fields if I search for them in other documents.

This is the method I use:

 public void checkIfCardExists() { Query query = cardInfo.whereEqualTo("CardNo", cardNumber) .whereEqualTo("Phone", userPhone); query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { boolean documentExists; if (task.isSuccessful()) { Log.d("QueryResult", "Is query result empty: " + task.getResult().isEmpty()); documentExists = !task.getResult().isEmpty(); }else { Log.e("QueryResult", "Error getting documents.", task.getException()); documentExists = false; } if(documentExists) { Log.d("QueryResult", "The document exists"); Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number found", Toast.LENGTH_SHORT).show(); userLeap = new UserLeap(userEmail, userPass, userName, userSurname, cardNumber, userPhone); registerUserLeap(userEmail, userPass); startActivity(new Intent(RegistrationLeap.this, Empty.class)); }else{ Log.d("QueryResult", "The document doesn't exist or there was an error retrieving it"); Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number not found", Toast.LENGTH_SHORT).show(); startActivity(new Intent(RegistrationLeap.this, Empty.class)); } } }); } 

And this is how my Firestore database looks like Firestore database

I added a photo to clarify about finding the first document

7
  • What do you mean through "wouldn't find the fields if I search for them in other documents"? And what exactly in this code doesn't work the way you expect? Commented Apr 3, 2020 at 16:30
  • I mean that if I enter card number and phone number that are located in the first document, it will find it and proceed with the code. But if those are located somewhere else, it won't find the,m and won't register the user Commented Apr 3, 2020 at 16:38
  • I mean that if I enter card number and phone number that are located in the first document, it will find it and proceed with the code. That's correct behavior. What do you mean through "located somewhere else"? Commented Apr 3, 2020 at 16:41
  • I added one more pic. The top document will be found but not the rest of the docs. How do I search through all the docs? Commented Apr 3, 2020 at 16:54
  • What the values in the other documents, are the same as in the first? Commented Apr 3, 2020 at 17:04

1 Answer 1

1

If you are using the following line of code:

Query query = cardInfo.whereEqualTo("CardNo", cardNumber) .whereEqualTo("Phone", userPhone); 

It means that you are telling Firestore to return all documents where the CardNo property holds the value of cardNumber AND the Phone property holds the value of userPhone. So if in your collection only one document satisfies this constraint, a single document will be returned. The other documents won't exist in the results. What you are doing now, is called filtering. However, if you want to get all documents, then you should remove both whereEqualTo() calls or directly use cardInfo which is a CollectionReference object. In this way, you aren't filtering anything. A CollectionReference object is basically a Query without any filters.

So using the last solution you can get all documents and you can also create the filtering on the client. This is not a recommended approach because getting all documents, will be very costly. For instance, if you have in your collection 1000 documents, you'll pay 1000 read operations to have them. So it's up to you to decide which one is better for you.

Sign up to request clarification or add additional context in comments.

4 Comments

I need only one document. But the problem is that my application wouldn't find anything else except for the first document. If I enter values from the second or third document it will return "doesn't exist" and will direct the flow to the wrong direction
It that case it means that the values you pass to the .whereEqualTo() do not match the one you have in your database. Try to use hardcoded values, does it work that way?
The values where indeed different, I tried to compare strings with numbers. Epic fail) Thank you for your answers, it helped!
Good to hear that is helped ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.