如果数据库中已存在同名文档,请设置错误

时间:2018-03-19 19:34:27

标签: android firebase google-cloud-firestore

我有一个用户可以创建新团队的活动。当用户提供团队名称和国家/地区并按下saveTeambutton时,saveTeam应检查三个条件是否匹配:teamNameText不为空且teamCountryText不为空。这些工作应该如他们应该的那样,但第三个却没有。我想检查给定的团队名称是否已存在于数据库中,如果存在,则给出错误。我用的是云端防火墙。这种情况确实会产生错误,但不会阻止团队被创建​​。

以下是代码的相关部分:

 public void saveTeam(){
    final String teamNameText = teamName.getText().toString();
    final String teamCountryText = teamCountry.getText().toString();
    final String username = user.getDisplayName();

    if (teamNameText.isEmpty()){
        teamName.setError("Team name is required");
        teamName.requestFocus();
        return;
    }




    if (teamCountryText.isEmpty()){
        teamCountry.setError("Team country is required");
        teamCountry.requestFocus();
        return;
    }


    db.collection("Teams").document(teamNameText).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.getResult().exists()){
                teamName.setError("Team with same name already exists");
                teamName.requestFocus();
            }
        }
    });


    db.collection("Users").document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            DocumentSnapshot documentSnapshot = task.getResult();
            if (documentSnapshot.get("User's team") == null){
                TeamMap data1 = new TeamMap(teamNameText, teamCountryText);
                db.collection("Teams").document(teamNameText).set(data1);
                //Need to add hash to teamNameText to prevent duplicates

                Map<String, Object> data = new HashMap<>();
                data.put("username", username);
                db.collection("Teams").document(teamNameText).collection("TeamUsers").document(uid).set(data);
                Map<String, Object> data2 = new HashMap<>();
                data2.put("User's team", teamNameText);

                db.collection("Users").document(uid).set(data2).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()){
                            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }else{
                Toast.makeText(getApplicationContext(), "You are already in a team", Toast.LENGTH_LONG).show();
            }
        }
    });
}

所以问题在于我从云端防火墙内部设置了错误。不会停止执行其余方法的方法。如果团队名称已存在,我如何以不创建团队的方式格式化方法?谢谢。

1 个答案:

答案 0 :(得分:0)

所以这是工作代码。在onComplete中检查状态后,我不得不简单地移动我想要执行的代码。

这是在按下按钮时执行的工作saveTeam方法:

 public void saveTeam(){
    final String teamNameText = teamName.getText().toString();
    final String teamCountryText = teamCountry.getText().toString();
    final String username = user.getDisplayName();

    if (teamNameText.isEmpty()){
        teamName.setError("Team name is required");
        teamName.requestFocus();
        return;
    }

    if (teamCountryText.isEmpty()){
        teamCountry.setError("Team country is required");
        teamCountry.requestFocus();
        return;
    }

    //Check if team with the same name already exists
    db.collection("Teams").document(teamNameText).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.getResult().exists()){
                teamName.setError("Team with same name already exists");
                teamName.requestFocus();

            }else{
                //If team with same name doesn't exist, save it
                db.collection("Users").document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        DocumentSnapshot documentSnapshot = task.getResult();


                        //Check if user is already in a team
                        if (documentSnapshot.get("User's team") == null){
                            TeamMap data1 = new TeamMap(teamNameText, teamCountryText);
                            db.collection("Teams").document(teamNameText).set(data1);

                            Map<String, Object> data = new HashMap<>();
                            data.put("username", username);
                            db.collection("Teams").document(teamNameText).collection("TeamUsers").document(uid).set(data);
                            Map<String, Object> data2 = new HashMap<>();
                            data2.put("User's team", teamNameText);

                            db.collection("Users").document(uid).set(data2).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()){
                                        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        }else{
                            Toast.makeText(getApplicationContext(), "You are already in a team", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
    });
}