Firebase Cloud Firestore:集合引用无效.集合引用必须具有奇数个段 [英] Firebase Cloud Firestore : Invalid collection reference. Collection references must have an odd number of segments

查看:27
本文介绍了Firebase Cloud Firestore:集合引用无效.集合引用必须具有奇数个段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code and getting an error :

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

推荐答案

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

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.

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

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.

这篇关于Firebase Cloud Firestore:集合引用无效.集合引用必须具有奇数个段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆