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

JavaAndroidFirebaseGoogle Cloud-Firestore

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Then you need change this:

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

for this:

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

You welcome ;)

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRelmView Question on Stackoverflow
Solution 1 - JavaDiego VenâncioView Answer on Stackoverflow
Solution 2 - JavaBob SnyderView Answer on Stackoverflow
Solution 3 - JavaVikash SharmaView Answer on Stackoverflow
Solution 4 - JavaRadu LinuView Answer on Stackoverflow
Solution 5 - JavaGeorges DamienView Answer on Stackoverflow