【问题标题】:How to Query Firestore and get specific documents in android如何在 android 中查询 Firestore 并获取特定文档
【发布时间】:2020-11-21 22:29:35
【问题描述】:

我在使用 firestore 查询 "whereEqualTo" 时遇到问题。我正在尝试从名为“参与者”的集合中获取图像,但是当文档不存在时,它应该使用 else 语句并移至更新文档的下一个活动。但是,当从 onComplete 侦听器中的集合中查询文档时,即 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);
                        }
                    });
                }
            }
        });

【问题讨论】:

    标签: android google-cloud-firestore android-query


    【解决方案1】:

    即使没有找到该文档,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 语句并将用于更新文档的代码放入其中。

    【讨论】:

    • 但是您提到的是文档参考。如何在文档参考中使用查询
    • 您不必这样做。我给出的示例只是为了向您展示即使没有文档存在,任务也是成功的。在您的情况下,您只需要检查 task.getResult() 返回的内容。如果文档不存在,它将为 null 或长度 QuerySnapshot 列表/数组为零。
    • 这是正确的,无论是否找到文档都不会改变查询特定信息的任务本身将成功完成的事实。在这种情况下,查询操作将完成,然后您需要调查查询返回的文档是否存在。因此,应该考虑 James 提供的 if/else 语句。
    • 您还可以编辑提供的代码,以便您可以考虑您的用例,这更像是一个模板来识别原始帖子中的行为。
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多