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 starts with a fixed-price diagnosis, then a fixed price for the fix before any fix work.
What breaks the path, and the fix
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));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 });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 segments04
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().
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.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.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.See scope and start
“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
Related: Firestore unsupported field value: undefined · Firebase missing or insufficient permissions · Firestore client is offline · Firestore ALREADY_EXISTS