Home > database >  If data not exists in the Firestore using Collection
If data not exists in the Firestore using Collection

Time:11-13

I just want to know if the user enter a value that did not exist for eg: XYZ654 that was not exists in Firestore.

Here's my code:

CollectionReference reference = firestore.collection("Users");
    reference.whereEqualTo("ReferCode", binding.refferalEd.getText().toString().toUpperCase()).get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot snapshot : task.getResult()) {
                            String RefreeMobile = snapshot.getString("Mobile");
                            Log.e(TAG, "mobile: "   RefreeMobile);
                            dialog.dismiss();


                            WriteBatch batch = firestore.batch();

                            // Some Code here. . .

                            
                        }
                    } else {
                        Log.e(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

If here binding.refferalEd the user was put the wrong referral that was not exist in the Firestore then I want to set Error in the EditText but Confused where I describe that:

Details Details

CodePudding user response:

CollectionReference reference = FirebaseFirestore.getInstance().collection("Users");
    reference.whereEqualTo("ReferCode", binding.refferalEd.getText().toString().toUpperCase()).get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot snapshot : task.getResult()) {
                        String RefreeMobile = snapshot.getString("Mobile");
                        Log.e(TAG, "mobile: "   RefreeMobile);
                        dialog.dismiss();


                        WriteBatch batch = firestore.batch();

                        // Some Code here. . .
                    }
                } else {
                    //put the error to edittext code here
                    //The error comes here as there is not document like that
                    Lod.i("Document not existing", task.getExeption()   "");
                }
            });

CodePudding user response:

To solve this issue, please use the following lines of code:

FirebaseFirestore firestore = FirebaseFirestore.getInstance();
CollectionReference reference = firestore.collection("Users");
reference.whereEqualTo("ReferCode", binding.refferalEd.getText().toString().toUpperCase()).get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    QuerySnapshot querySnapshot = task.getResult();
                    if (!querySnapshot.isEmpty()) {
                        for (QueryDocumentSnapshot snapshot : querySnapshot) {
                            String RefreeMobile = snapshot.getString("Mobile");
                            Log.e(TAG, "mobile: "   RefreeMobile);
                            dialog.dismiss();

                            WriteBatch batch = firestore.batch();
                            // Some Code here. . .
                        }
                    } else {
                        Log.e(TAG, "wrong referral.");
                    }
                } else {
                    Log.e(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

As you can see, you have to explicitly check if querySnapshot.isEmpty(). In this way, you can set "wrong referral" to your TextView.

  • Related