17

I'm getting the rather simple error:

Document references must have an even number of segments.

I'm aware of what it is telling me and how to fix it, however it is giving me this error on a collection reference.

CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);

In my database the path is (COLLECTION) < DOCUMENTS > (COLLECTION) < DOCUMENTS > I'm trying to get all contacts for a user at users/{id}/contacts but it just throws this error, any ideas?

getFirebaseInstance is a method I've created to always get the current

FirebaseFirestore.getInstance();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
martinseal1987
  • 1,862
  • 8
  • 44
  • 77

4 Answers4

25

Your userId variable probably has a slash in it. Document ids can't have slashes, since they are interepreted as dividers between collections and documents when forming the "path" to a document.

It's also possible that the string may be empty, which is invalid.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    thats a good shout but unfortunately not the userId is a firebase auto generated id heres an example 37yntLaKSGPgNVrRs18M9GDRLy42 – martinseal1987 Jul 05 '18 at 16:31
  • So are you absolutely certain the error message is happening with the code you're showing? Please show all the relevant code and logs that demonstrate the values of everything at the time of the error. – Doug Stevenson Jul 05 '18 at 16:35
  • Yes this is the exact piece of code that's having an issue it doesn't crash on first run but if I leave my app and come back to it later it crashes also if I copy and paste it somewhere earlier In My code it will crash with the same error and point me to here, it seems like the user id string I'm using (its a shared preference) is being cleared at some point apparently this can happen when two threads try to access it so I'll swap this to get the string from a database and if the issue persists I'll update – martinseal1987 Jul 06 '18 at 08:43
  • this seems to be fixed and was caused by the userId string being empty but i'm going to mark your answer as correct as its the closest to it and others finding this will know that it must be the variable here many thanks – martinseal1987 Jul 06 '18 at 21:35
  • 4
    Edited the answer. I also filed a bug with the Firestore client team asking for a more helpful error message in the event that a collection or document id is empty. – Doug Stevenson Jul 07 '18 at 03:25
1

I found the same problem, this is what solved my problem :

I tried to set a document using the :

setDoc()

without providing the document id.

So I used :

addDoc()

so firebase itself provide an id for document.

The lecon is that : using set you must provide an ID, using add you do not have provide the ID firebase do it for you.

Thanks !

Ibrahim Kelly
  • 548
  • 1
  • 4
  • 9
0

Because neither the above nor other similar posts helped me..

I got this error when calling set data and then immediately after .getDocument(...) (i.e. reading data). When I called my read data method in the completion handler of the setData method then the error message disappeared.

T. Fo
  • 199
  • 2
  • 6
0

i'm not sure that's your case ,But, if you have a function like this, that brings data by "Id" from your firebase , just add a slash "/" after your collection's name

getDetailProduct(id: string): Observable<Interface_name> {
const productsDocuments = this.angularFirestore.doc<Interface_name>(
  'collection_name/' + id  //here after your collection name add "/"
);
return productsDocuments.snapshotChanges().pipe(
  map((changes) => {
    const data = changes.payload.data() as Interface_name;
    const id = changes.payload.id;
    return { id, ...data };
  })
);

}