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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46639058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:25:35  来源:igfitidea点击:

Firebase Cloud Firestore : Invalid collection reference. Collection references must have an odd number of segments

javaandroidfirebasegoogle-cloud-firestore

提问by Relm

I have the following code and getting an error :

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

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

And the code :

和代码:

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());
            }
        });
    }

采纳答案by Bob Snyder

Hierarchical data structures and subcollections are described in the documentation. A collection contains documents and a document may contain a subcollection. The structure is always an alternating pattern of collections and documents. The documentation contains this description of an example:

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

Notice the alternating pattern of collections and documents. Your collections and documents must always follow this pattern. You cannot reference a collection in a collection or a document in a document.

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

Thus, a valid path to a collection will always have an odd number of segments; a valid path to a document, an even number. Since your code is trying to query a collection, the path length of four is invalid.

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

回答by Diego Venancio

Then you need change this:

然后你需要改变这个:

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

for this:

为了这:

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

You welcome ;)

没关系 ;)

回答by Vikash Sharma

You are missing collection reference. i.e db.collection(** This is getting null **).

您缺少集合参考。即 db.collection(** 这是空的 **)。

回答by Radu Linu

I've encountered this issue when I provided a wrong entity_Id.

我在提供错误的 entity_Id 时遇到了这个问题。

Instead of dojo/default/datasets/fe67ec58-6208-4234-a4ee-98c5dce4665f, I've provided fe67ec58-6208-4234-a4ee-98c5dce4665fand now is working fine.

而不是dojo/default/datasets/fe67ec58-6208-4234-a4ee-98c5dce4665f,我提供了fe67ec58-6208-4234-a4ee-98c5dce4665f,现在工作正常。