java - Firestore-检查用户名是否已存在

标签 java android firebase google-cloud-firestore

<分区>

我需要 Firestore 方面的帮助。我有一个 AllUsers 数据集合,包含每个用户信息的用户 ID 文档。我想检查用户名是否已经存在;//“该用户名已经存在”。我该怎么做?

` setupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final String user_name = setupName.getText().toString();

            Map<String, Object> commentsMap = new HashMap<>();
            commentsMap.put("user_id", user_id);
            commentsMap.put("timestamp", FieldValue.serverTimestamp());
            commentsMap.put("user_name",user_name);
            firebaseFirestore.collection("AllUsers").document(user_id).set(commentsMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Intent asda=new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(asda);
                        finish();
                   }

                }
            });

`

最佳答案

希望这会有所帮助。

setupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = setupName.getText().toString();

                CollectionReference usersRef = firestore.collection("Users");
                Query query = usersRef.whereEqualTo("username", userName);
                query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful()){
                            for(DocumentSnapshot documentSnapshot : task.getResult()){
                                String user = documentSnapshot.getString("username");

                                if(user.equals(userName)){
                                    Log.d(TAG, "User Exists");
                                    Toast.makeText(MainActivity.this, "Username exists", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }

                        if(task.getResult().size() == 0 ){
                            Log.d(TAG, "User not Exists");
                            //You can store new user information here

                        }
                    }
                });
            }
        });

关于java - Firestore-检查用户名是否已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861391/

相关文章:

java - 如何修复 QueryFullProcessImageName 抛出 "A device attached to the system is not functioning"?

java - JCR SQL2 查询显然没有提供任何结果

angular - 将 FirebaseListObservable 结果转换为对象

android - Action Bar Style导致Action Bar消失

ios10、Swift 3 和 Firebase 推送通知 (FCM)

ios - 从通知中心托盘中删除特定的推送通知

java - jsp中如何通过session访问对象

java - 设置异常(exception)

android - 如何检测Android应用何时进入后台并回到前台

android - Android 6 中的 adb 在哪里?它被删除了吗?