【问题标题】:Checking Firestore Collection element exist or not检查 Firestore 集合元素是否存在
【发布时间】:2021-12-22 16:56:08
【问题描述】:

我想检查 输入的文本 (EditText) 元素是否存在,如果存在则使用 Firestore 集合或获取剩余元素注意:我们没有文档 ID

如果我们的用户集合存在移动设备,我想要移动设备然后获取文档 ID 将 使用 AutoID 随机生成的剩余元素,并忽略像 撤销请求这样的附加集合

我在我的活动中编写此代码

private void fetchDatafromFirestoreCollection() {

    firestore.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
            if (error != null) {
                if (dialog.isShowing())
                    dialog.dismiss();

                Log.e(TAG, "Error is: ", error);
                return;

            }
            for (DocumentChange documentChange : value.getDocumentChanges())
        }
    });

}

【问题讨论】:

  • 请阅读说明以了解我想说什么
  • 我不确定我是否理解。您要检查是否存在的确切数据是什么?
  • 这个问题似乎不清楚。您能否详细描述问题并清楚说明您想要达到的目标?
  • 我要获取所有文件来检查ABC123在哪里存在或不给他推荐金额

标签: java android firebase android-studio google-cloud-firestore


【解决方案1】:

根据这条评论:

我想检查用户集合是否存在ReferCode“ABC123”如果存在谁是用户然后我获取手机或其他详细信息

还有这条评论:

我想获取所有文件来检查ABC123在哪里,或者不给他推荐金额。

要解决这个问题,您必须使用如下所示的查询:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference usersRef = db.collection("Users");
usersRef.whereEqualTo("ReferCode", "ABC123").get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    String mobile = document.getString("Mobile");
                    Log.d("TAG", mobile);

                    //Do what you need to do with the phone number
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

logcat 中的结果将是:

9876543210

【讨论】:

  • 您如何回复我的评论是 Stackoverflow 的任何内置功能手动复制和粘贴文本
  • 我只在你的 cmets 前面使用了&gt;,它们是作为复制/粘贴添加的。
  • 当有人输入错误的ReferCode 时我会怎么做,因为我尝试了addOnFailureLister 但在输入错误的代码时没有执行数据库中不存在的操作。 ? ? ?
  • 不,只有在 Firebase 服务器拒绝请求时才会调用 onFailure。不返回文件不会触发
  • 所以我用来检查是否不会返回任何文档我想通过binding.referEd.setError("Wrong refer code")在编辑文本中设置错误
【解决方案2】:

制作这样的文档结构:

    DocumentReference docRef = db.collection("Users").document("123456789");
                docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                    @Override
                    public void onEvent(DocumentSnapshot snap, FirestoreException fe) {
                        if (snap.exists()) {
                            //exists
                        for (DocumentChange dc : snap.getDocumentChanges()) {
                            //Store exist information to your Object model.
                            UserInfo userinfo = dc.getDocument().toObject(UserInfo.class);
                         Log.e(TAG, "userinfo: "+userinfo.getCompany);
                        }
                        } else {
                            //not exists
                        }
                    }
    
                });

希望它的工作..

【讨论】:

  • 我没有文档 ID
猜你喜欢
  • 1970-01-01
  • 2021-05-05
  • 2010-11-23
  • 2021-09-15
  • 2019-04-19
  • 2019-01-21
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多