0

I having trouble in using the firestore query "whereEqualTo" . i am trying to get the images from a collection called "Participants" but when the documents does not exist it should use the else statement and move to next activity where the document is updated. But when querying the document from collection in onComplete listener i.e Task the document does not exit but the code is running into if(task.isSuccessful){} and not using the else statement. my code: Can i anyone help me with this thanks

 //***********BattlePost1**************

        mFirestore.collection("OpenBattle")
                .document(battlesPost_creator)
                .collection("BattlePost")
                .document(battlePost_id)
                .collection("Participants")
                .document(UserId)
                .collection("Posts").whereEqualTo("battle_post_count", 1)
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
                    for (QueryDocumentSnapshot document : task.getResult()){
                        Glide.with(mContext).load(document.get("imageuri")).into(battle_creator_postImage1);
                        battle_points1.setText(String.valueOf(document.get("numberOfLikesPoints")) + "Points");
                    }

                }else {
                    Log.d(TAG, "onComplete: Error getting the first document");
                }
            }
        });

        //**************BattlePost2***************

        mFirestore.collection("OpenBattle")
                .document(battlesPost_creator)
                .collection("BattlePost")
                .document(battlePost_id)
                .collection("Participants")
                .document(UserId)
                .collection("Posts")
                .whereEqualTo("battle_post_count", 2)
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                        Glide.with(mContext).load(document.get("imageuri")).into(battle_creator_postImage2);
                        battle_points2.setText(String.valueOf(document.get("numberOfLikesPoints")) + "Points");
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                    //Can create a 2nd post
                    Glide.with(mContext).load(R.drawable.ic_add_circle_black_24dp).into(battle_creator_postImage2);
                    battle_creator_postImage2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(mContext, TemplateActivity.class);
                            intent.putExtra(getString(R.string.battle_add_post),battlePost_id);
                            intent.putExtra("BattlePost_Pub", battlesPost_creator);
                            intent.putExtra("Battle_contest", 2);
                            intent.putExtra("Battle_tittle", battle_tittle);
                            intent.putExtra("Battle_timeEnd", battle_timeEnd);
                            startActivity(intent);
                        }
                    });
                }
            }
        });

enter image description here

AndroidRocket
  • 418
  • 3
  • 14

1 Answers1

0

Even if the document is not found, task.isSuccesful() will return true because it has completed the task of querying and found that no such document exists. So, instead, you should check if the document exists. Have a look at this from the documentation-

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            //See the part below.
            if (document.exists()) {
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

You will have to write another else statement and put your code for updating the document in it.

James
  • 75
  • 7
  • 1
    But what you mentioned is a document reference . How can I use a query in document reference – AndroidRocket Aug 01 '20 at 20:48
  • 1
    You don't have to. I gave the example just to show you that the task is successful even if no document exists. In your case, you just need to check what the task.getResult() returns. If the doc doesn't exist, it would be null or the length QuerySnapshot list/array would be zero. – James Aug 02 '20 at 12:23
  • This is correct, whether or not the document is found will not change the fact that the task itself, of querying for the particular information, will complete successfully. In this case, the operation of querying will complete, and then you will need to investigate whether or not the query returns the document as existing or not. Hence the if/else statement James has provided should be taken into account. – KevinH Aug 09 '20 at 01:35
  • You can also edit the code provided so that you may account for your use case, this is more so a template to identify the behavior in the original post. – KevinH Aug 09 '20 at 01:45