I want to check the entered text (EditText) element is exist or not using Firestore Collection if Exists where exists or fetch the remain elements Note: we don't have document id
I want to mobile if our user collection the mobile exists then fetch the remain elements the document id will Random using AutoID and ignore Additional Collections like Withdraw Requests
I write this code in my activity
private void fetchDatafromFirestoreCollection() {
firestore.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null) {
if (dialog.isShowing())
dialog.dismiss();
Log.e(TAG, "Error is: ", error);
return;
}
for (DocumentChange documentChange : value.getDocumentChanges())
}
});
}
CodePudding user response:
According to this comment:
I want to to check in Users collection is ReferCode "ABC123" exists if exists who is the user then I fetch the mobile or other details
And this comment:
I want to fetch all the documents to check where ABC123 exists or not to give him refferal amount.
To solve this, you have to use a query that looks like this:
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference usersRef = db.collection("Users");
usersRef.whereEqualTo("ReferCode", "ABC123").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String mobile = document.getString("Mobile");
Log.d("TAG", mobile);
//Do what you need to do with the phone number
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
The result in the logcat will be:
9876543210
CodePudding user response:
Make the document structure like this one:
DocumentReference docRef = db.collection("Users").document("123456789");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot snap, FirestoreException fe) {
if (snap.exists()) {
//exists
for (DocumentChange dc : snap.getDocumentChanges()) {
//Store exist information to your Object model.
UserInfo userinfo = dc.getDocument().toObject(UserInfo.class);
Log.e(TAG, "userinfo: " userinfo.getCompany);
}
} else {
//not exists
}
}
});
Hope its work..