80

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());
            }
        });
    }
Relm
  • 7,923
  • 18
  • 66
  • 113

6 Answers6

70

Then you need replace it:

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

with it:

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

You're welcome ;)

--UPDATE 2023--

If you are using the new Firebase Modular, then remember about the reference:

Firebase requires changing references: "collectionName" / "documentName" / "collectionName" / "documentId", etc.

setDoc(doc(db, "collectionName", userID, "anotherCollectionName", documentID), ...

ref

Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68
62

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.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • Ok, I need something like this : app/users/userId/notifications/{auto-gen-id}/document(The Notification Object). How do I create this sort of a thing in the console or through code? – Relm Oct 09 '17 at 06:07
  • @Reim: The console supports creating the structure you want. – Bob Snyder Oct 09 '17 at 13:33
  • 34
    Not that anyone else would be dumb enough to miss this, but one other simple way you can end up with this error is if you run a get on a blank document reference, so check your variable and make sure its not an empty string... – Chris Apr 27 '18 at 20:41
6

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

Vikash Sharma
  • 539
  • 8
  • 13
1

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

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

Radu Linu
  • 1,143
  • 13
  • 29
0

I've encountered this issue when I provided an entity_Id that contains the "/" character ( my value was N/A ) when i was trying to read documentReference (DocumentReference docRef2 = fireStoreDb.Collection("Ass").Document(ass.Tel.ToString()) .Collection("vehicules").Document(ve.Immatriculation);). Here, the value ve.Immatriculation equals to N/A, and that was the problem.

  • so what is your solution? – Saar Davidson Oct 05 '20 at 00:12
  • ensure that you insert data as document Id without the caracter / . For my part, i have a batch in .net (c#) that insert data in FireBase; so i use this line Immatriculation = car.Immatriculation.Replace("/", "").Replace(".", "").Trim(). to replace the / caractere. Here is the way i use the value as document id : DocumentReference docRef2 = fireStoreDb.Collection("asss").Document(ass.Tel.ToString()) .Collection("vehicules").Document(ve.Immatriculation); – Georges Damien Oct 06 '20 at 14:22
0

In my case collection name is empty was causing the crash

FirebaseFirestore.getInstance().collection("").add(taravih)
Mohd Qasim
  • 896
  • 9
  • 20