Skip to content

Firestore error

Firestore: “Invalid document reference” (even number of segments)

Invalid document reference is the Firestore error for a doc() path with an odd number of segments. A document path alternates collection and document, like users/abc, so it always has an even count. The usual cause is an empty id or a slash inside one.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

Log the path you pass to doc(). If an id is empty or undefined, wait until it exists. If an id contains a slash, such as a URL or a date, encode it or pick another id. Use collection() for paths with an odd number of segments, like users/abc/orders.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

What breaks the path, and the fix

  1. 01

    The id is an empty string

    “If the final path has an odd number of segments and does not point to a document.” Firebase, doc() exceptions

    An empty uid makes doc(db, 'users', uid) a one-segment path, so it throws this error. An undefined uid throws a different error, and a template string such as users/${uid} builds users/undefined: a valid path to the wrong document, with no error at all. Wait for sign-in before you build the path, and throw a clear error if the id is missing.

    // Web SDK
    import { doc, getDoc } from 'firebase/firestore';
    
    if (!uid) throw new Error('No user id yet');
    const snap = await getDoc(doc(db, 'users', uid));
  2. 02

    The id contains a slash

    Each slash starts a new segment, so an id like 2026/11/09 or a full URL splits into several, and a double slash in a built path fails too. Encode the value, or use a hash of it for long values. Don’t just swap slashes for another character: two different values can end up with the same id, and setDoc then overwrites one with the other.

    const id = encodeURIComponent(url); // reversible, so no two urls share an id
    await setDoc(doc(db, 'links', id), { url });
  3. 03

    A collection path went into doc()

    “Notice the alternating pattern of collections and documents. Your collections and documents must always follow this pattern.” Firebase, Cloud Firestore data model

    users/abc/orders points to a subcollection, which has three segments. Use collection() for it, and doc() for a document such as users/abc/orders/o1.

    const orders = collection(db, 'users', uid, 'orders'); // 3 segments
    const order = doc(db, 'users', uid, 'orders', orderId); // 4 segments
  4. 04

    The same mistake the other way round

    “If the final path has an even number of segments and does not point to a collection.” Firebase, collection() exceptions

    collection() with a document path, such as users/abc, throws the matching error about an even number of segments. Drop the last segment or switch to doc().

  5. 05

    A document can’t sit inside a document

    “You cannot reference a collection in a collection or a document in a document.” Firebase, Cloud Firestore data model

    Data nested under a document goes in a field or in a subcollection, never straight under another document. If the path was built for nesting, put a collection name between the two ids.

Checked against the vendor’s docs on : Firebase docs: Cloud Firestore data model; Firebase JS SDK reference: firestore.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Firestore unsupported field value: undefined · Firebase missing or insufficient permissions · Firestore client is offline · Firestore ALREADY_EXISTS

Questions

More on this error

Does the Admin SDK throw the same error?

The Admin SDK also checks the path, with its own wording. There the call is db.collection('users').doc(uid) or db.doc('users/' + uid), and the same fixes apply: no empty ids, no slashes inside ids, and doc() only for document paths.

Can I use a slash in a document id at all?

No. A slash always splits the path. Encode the value or store it in a field, and use another value as the id.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.