Firebase Cloud Firestore:Invalid Collection Reference. Collection References Must Have an Odd Number of Segments

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

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.

Uncaught (in promise) FirebaseError: Invalid collection reference. Collection references must have an odd number of segments

"Accounts/" + localStorage.getItem('Id') + "/RequestsReceived", id

This path is pointing towards a document in 'RequestsReceived' sub-collection. So you should be using doc() to create a DocumentReference.

Also you don't need to fetch a document before deleting as long as you have the reference:

// ensure all values are defined
console.log(localStorage.getItem('Id'), id);

// use doc()
var ref2 = doc(db, "Accounts/" + localStorage.getItem('Id') + "/RequestsReceived", id);

await deleteDoc(ref2);

FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but services/p0dnjzr7HZrcp6Mifiqh has 2

Fixed it by changing this

return this.afStore.collection(`services`).doc(id)

into

return this.afStore.collection('services', ref => ref.where('id', '==', id))

`

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

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.

Getting Invalid doc ref. Doc ref must have an even number of segments, Error when writing batch doc to Firebase Firestore V9 Angularfire

After some searching, I found a solution in the Firebase documentation that you can create a document reference with an auto-generated ID, then use the reference later. Like so:

const batch = writeBatch(this.db);

const ref = collection(this.db, 'collection/document/collection1/document/collection2');

batch.set(doc(ref), data);

await batch.commit()

Firestore: Invalid document reference. Document references must have an even number of segments,

The error message:

Invalid document reference. Document references must have an even number of segments, but userFavorites/FRbYxmNpSBcda6XOrQUjukvFvVb2/q7eLxtZfhG3g6Pd1bYY4 has 3

is telling you that you built a path to a document:

userFavorites/FRbYxmNpSBcda6XOrQUjukvFvVb2/q7eLxtZfhG3g6Pd1bYY4

which doesn't look like a document at all, since it has three path segments:

  1. userFavorites
  2. FRbYxmNpSBcda6XOrQUjukvFvVb2
  3. q7eLxtZfhG3g6Pd1bYY4

This is the line of code that built your path:

Firestore.instance.collection("userFavorites/$userId").document(id)

Since we can't see your data, it's hard to tell what you actually meant to do here. But in any event, Firestore is taking "userFavorites" to be the name of a top level collection, "FRbYxmNpSBcda6XOrQUjukvFvVb2" is the name of a document in that collection, and "q7eLxtZfhG3g6Pd1bYY4" is taken to mean a subcollection under that document. If you meant something else, you'll have to figure out how to build the path to that document to query it.



Related Topics



Leave a reply



Submit