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

I have the following code and 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()); } }); } 
+29
source share
3 answers

Hierarchical data structures and subcollections are described in the documentation . A collection contains documents, and a document may contain a nested collection. The structure always alternates with collections and documents. The documentation contains this example description:

Pay attention to the alternating pattern of collections and documents. Your collections and documents should always follow this pattern. You cannot reference a collection in a collection or a document in a document.

Thus, a valid collection path will always have an odd number of segments; Valid document path, even number. Because your code is trying to request a collection, a path length of four is not allowed.

+26
source

Then you need to change this:

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

for this:

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

You greet;)

+29
source

You are missing a collection link. those. db.collection (** This becomes null **).

-1
source

Source: https://habr.com/ru/post/1272445/


All Articles