I need help with Firestore queries. I have a all_users
data collection, user id-documents with each user information. firestore database image i want to check i
The following query returns all users with provided usernameToCheck. If username is unique then youll get only one documentSnapShot.
Query mQuery = mFirebaseFirestore.collection("all_users")
.whereEqualTo("username", "usernameToCheck");
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentSnapshot ds: documentSnapshots){
if (ds!=null){
String userName = document.getString("username");
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " +userName );
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
To solve this, please use the following lines of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference allUsersRef = rootRef.collection("all_users");
Query userNameQuery = allUsersRef.whereEqualTo("username", "userNameToCompare");
userNameQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if (document.exists()) {
String userName = document.getString("username");
Log.d(TAG, "username already exists");
} else {
Log.d(TAG, "username does not exists");
}
}
} else {
Log.d("TAG", "Error getting documents: ", task.getException());
}
}
});
In which userNameToCompare
is of type String and is the user name of the user with which you want to make the comparison.
Since I am unable to comment, I am typing this as an answer instead:
I want to know how does your logs look like when the snapshotListener run.
As in, which of the above logs under your conditions are being printed on the console when the spanshotListener runs.
Also, you wrote you are getting no result so is it safe to assume that you are expecting to see the Toasts but are not able to?
Also, after skimming through your code, I have put comments based on this documentation :
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (documentSnapshots != null){
// you are checking here whether your querySnapshot is not null
// instead of checking whether it is null or empty.
// taking a wild guess here but could try to modify
// your if condition as follows -->
// if (documentSnapshots.isEmpty()) {
Log.d(TAG, "onEvent: username does not exists");
Toast.makeText(getActivity(), "Username is available", Toast.LENGTH_SHORT).show();
}
// below as well documentSnapshots is assumed to be not null,
// hence all the more reason I am taking the above wild guess
for (DocumentSnapshot ds: documentSnapshots){
if (ds.exists()){
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " + ds.toObject(Users.class).getUsername());
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
I am not able to debug any further on the data you have provided. But this link might help you more.