Firebase Cloud Firestore:集合引用无效。集合引用必须有奇数个段

新手上路,请多包涵

我有以下代码并收到错误:

 Invalid collection reference. Collection references must have an odd number of segments

和代码:

 private void setAdapter() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("app/users/" + uid + "/notifications").get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    Log.d("FragmentNotifications", document.getId() + " => " + document.getData());
                }
            } else {
                Log.w("FragmentNotifications", "Error getting notifications.", task.getException());
            }
        });
    }

原文由 Relm 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 400
2 个回答

文档 中描述了分层数据结构和子集合。集合包含文档,文档可能包含子集合。该结构始终是集合和文档的交替模式。该文档包含示例的描述:

注意集合和文档的交替模式。您的收藏和文档必须始终遵循此模式。您不能在集合中引用集合或在文档中引用文档。

因此,一个集合的有效路径总是有奇数个段;文档的有效路径,偶数。由于您的代码正在尝试查询集合,因此路径长度为 4 是无效的。

原文由 Bob Snyder 发布,翻译遵循 CC BY-SA 3.0 许可协议

然后你需要更换它:

 db.collection("app/users/" + uid + "/notifications")...

用它:

 db.collection("app").document("users").collection(uid).document("notifications")

别客气 ;)

原文由 Diego Venâncio 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题