0

我在使用 firestore 查询 "whereEqualTo" 时遇到问题。我正在尝试从名为“参与者”的集合中获取图像,但是当文档不存在时,它应该使用 else 语句并移至更新文档的下一个活动。但是,当从 onComplete 侦听器 ie Task 中的集合中查询文档时,文档不会退出,但代码正在运行到 if(task.isSuccessful){} 中并且不使用 else 语句。我的代码:谁能帮我这个谢谢

 //***********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);
                        }
                    });
                }
            }
        });

在此处输入图像描述

4

1 回答 1

0

即使没有找到该文档,task.isSuccesful()也会返回true,因为它已经完成了查询任务,发现不存在该文档。因此,您应该检查文档是否存在。从文档中查看这个 -

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());
        }
    }
});

您将不得不编写另一条else语句并将用于更新文档的代码放入其中。

于 2020-08-01T08:20:50.927 回答