1

Look I have my firestore database and I want to check if one of my users documents already has the username that the user typed in into the textview

 usersRef = db.collection("users");
 userNameTextView = findViewById(R.id.setup_username);
 String username = userNameTextView.getText().toString();
 Query query = usersRef.orderBy("username").whereEqualTo("username", userNameTextView.getText().toString());
if(query.get().isSuccessful()) {
            Toast.makeText( this, "Username is taken...", Toast.LENGTH_SHORT ).show();
        }

And this is the database structure just so you know:

This is how the database is  ordered

But somehow it isn´t working has somebody got an idea ?

Lucas Goldner
  • 617
  • 9
  • 29
  • Since we can't see the data you're querying, we can't tell if you're doing something wrong. Please edit the question to be clear about what exactly you expect this query to return, using specific examples. I suggest hard coding all strings, and showing a screenshot of the documents that should match your query. – Doug Stevenson May 25 '20 at 21:01
  • I added the database screenshot – Lucas Goldner May 25 '20 at 21:05
  • Now, edit your code to hard code the query parameters. We don't know what `userNameTextView.getText().toString()` returns. It might not be what you expect. – Doug Stevenson May 25 '20 at 21:06
  • It is a TextView so it returns a String ? – Lucas Goldner May 25 '20 at 21:09
  • You tell us. We can't see what's in your TextView. As I said, you should hard code the value in the query to help us know if you're actually querying the thing you expect. Also consider logging all relevant values before you pass them along to an API so you can be certain about what's happening. – Doug Stevenson May 25 '20 at 21:46

1 Answers1

1

You're only checking whether the task is successful which should only mean that you successfully communicated with the server. You should also be checking the size of the result to determine if any matching documents were found.

All of this should happen within a listener callback since you're waiting on a response from the server:

db.collection("myCollection")
    .whereEqualTo("someField", "value")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { // Add the listener callback
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            // Check if our task successfully communicated with the server
            if(task.isSuccessful()) {

                // Check the size of the result to see if any matches were found
                if(task.getResult().size() == 0){
                    // No document exists containing the searched value
                }else{
                    // A document already exists containing the searched value
                }

            }else{
                Log.e("MyLogTag", "There was an error querying the documents.", task.getException());
            }
        }
    });
Sammy T
  • 1,924
  • 1
  • 13
  • 20