Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-sheep-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': patch
---

Fix: Corrected misleading error message when doc() is called with undefined.
8 changes: 7 additions & 1 deletion packages/firestore/src/lite-api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,12 @@ export function doc<AppModelType, DbModelType extends DocumentData>(
path?: string,
...pathSegments: string[]
): DocumentReference<AppModelType, DbModelType> {
if (parent === undefined) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Function doc() cannot be called with an undefined first argument.'
);
}
parent = getModularInstance(parent);

// We allow omission of 'pathString' but explicitly prohibit passing in both
Expand All @@ -651,7 +657,7 @@ export function doc<AppModelType, DbModelType extends DocumentData>(
) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Expected first argument to collection() to be a CollectionReference, ' +
'Expected first argument to doc() to be a CollectionReference, ' +
'a DocumentReference or FirebaseFirestore'
);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ describe('doc', () => {

it('validates path', () => {
return withTestDb(db => {
expect(() =>
// @ts-ignore
doc(undefined, 'coll/doc')
).to.throw(
'Function doc() cannot be called with an undefined first argument.'
);
expect(() =>
// @ts-ignore
doc({}, 'coll/doc')
).to.throw(
'Expected first argument to doc() to be a CollectionReference, a DocumentReference or FirebaseFirestore'
);
expect(() => doc(db, 'coll')).to.throw(
'Invalid document reference. Document references must have an even ' +
'number of segments, but coll has 1.'
Expand Down
Loading