简体   繁体   中英

Document references must have an even number of segments error on a collection reference

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

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.

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

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

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 !

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM