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

Relm picture Relm · Oct 9, 2017 · Viewed 43.6k times · Source

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

Answer

Diego Venâncio picture Diego Venâncio · Jan 7, 2018

Then you need change this:

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

for this:

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

You welcome ;)