0

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

Details Details

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()) } }); } 
6
  • Please Read Description to Understand What I'm trying to say Commented Nov 10, 2021 at 2:46
  • 1
    I'm not sure I understand. What is the exact data you want to check for existence? Commented Nov 10, 2021 at 6:07
  • The question seems to be unclear. Can you describe the question in detail and state clearly what you want to achieve? Commented Nov 10, 2021 at 8:19
  • @AlexMamo I want to to check in Users collection is Refferal "ABC123" exists if exists who is the user then i fetch the mobile or other details Commented Nov 10, 2021 at 13:34
  • I want to fetch all the documents to check where ABC123 exists or not to give him refferal amount Commented Nov 10, 2021 at 13:35

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

7 Comments

how you reply with my comment is that any inbuilt feature of Stackoverflow of Manually Copy and paste the text
I only used > in front of your comments, that were added as copy/paste.
what will i do when someone enters the wrong ReferCode that was not exists in the database because i tried addOnFailureLister but that was not executed when enters the wrong code. ? ? ?
No, onFailure will be called only if Firebase servers reject the request. It will not be triggered if no documents will be returned
so what i used to check if no documents will be returned i want to set error in edit text by binding.referEd.setError("Wrong refer code")
|
0

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..

1 Comment

i don't have the document id

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.