【问题标题】:Firestore : query all documents from a collectionFirestore:查询集合中的所有文档
【发布时间】:2018-05-09 07:20:59
【问题描述】:

我的 Firestore 结构如下所示:

-(coll)users
    -(doc)uniqueID  
    name
    email  
    (coll)clients
        -(doc)uniqueID
        clientName
        clientEmail  

我想要达到的目标如下:

  1. 用户通过 FirebaseUI 身份验证流程登录
  2. 如果用户(从 Auth 恢复的 uid)在 firestore db 中不存在,我创建一个由他的 uid 命名的文档
  3. 获得 uid 后,我会运行查询以加载客户端集合,以便使用 RecyclerView 将它们显示到列表中(如果集合为空,则用户尚未创建任何客户端,但我会显示一个空列表屏幕)

我尝试按照documentation 使用下面的代码进行查询:

    clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
            .document(mUid)
            .collection(FIRESTORE_COLLECTION_CLIENTS);

    clientsCollection
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()){
                        for (DocumentSnapshot document: task.getResult()){
                            Log.d(LOG_TAG, document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d(LOG_TAG, "error getting documents: ", task.getException());
                    }
                }
            });

我收到以下RuntimeException

java.lang.NullPointerException: Provided document path must not be null.

即使客户端集合退出,其中包含一些由唯一 uid 命名的文档,我也会得到这个。

感谢您提供任何线索! :)

【问题讨论】:

    标签: android firebase-authentication google-cloud-firestore


    【解决方案1】:

    当您运行第一条语句时,错误消息表明mUidnull。这很可能意味着您在用户登录之前运行此代码。

    确保仅在用户登录后调用此代码,例如来自AuthStateListener.onAuthStateChanged()

    FirebaseAuth.getInstance().addAuthStateListener(new AuthStateListener() {
        public void onAuthStateChanged(FirebaseAuth auth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
                        .document(user.getUid())
                        .collection(FIRESTORE_COLLECTION_CLIENTS);
    
                clientsCollection
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()){
                                    for (DocumentSnapshot document: task.getResult()){
                                        Log.d(LOG_TAG, document.getId() + " => " + document.getData());
                                    }
                                } else {
                                    Log.d(LOG_TAG, "error getting documents: ", task.getException());
                                }
                            }
            }
        }
    })
    

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 2018-07-07
      • 2021-05-30
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多