Skip to content

Commit 1c9d060

Browse files
committed
Corrected misleading error message when doc() is called with undefined.
1 parent 578686b commit 1c9d060

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/firestore/src/lite-api/reference.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ export function doc<AppModelType, DbModelType extends DocumentData>(
627627
path?: string,
628628
...pathSegments: string[]
629629
): DocumentReference<AppModelType, DbModelType> {
630+
if (parent === undefined) {
631+
throw new FirestoreError(
632+
Code.INVALID_ARGUMENT,
633+
'Function doc() cannot be called with an undefined first argument.'
634+
);
635+
}
630636
parent = getModularInstance(parent);
631637

632638
// We allow omission of 'pathString' but explicitly prohibit passing in both
@@ -651,7 +657,7 @@ export function doc<AppModelType, DbModelType extends DocumentData>(
651657
) {
652658
throw new FirestoreError(
653659
Code.INVALID_ARGUMENT,
654-
'Expected first argument to collection() to be a CollectionReference, ' +
660+
'Expected first argument to doc() to be a CollectionReference, ' +
655661
'a DocumentReference or FirebaseFirestore'
656662
);
657663
}

packages/firestore/test/lite/integration.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ describe('doc', () => {
285285

286286
it('validates path', () => {
287287
return withTestDb(db => {
288+
expect(() => doc(undefined as any, 'coll/doc')).to.throw(
289+
'Function doc() cannot be called with an empty first argument.'
290+
);
291+
expect(() => doc({} as any, 'coll/doc')).to.throw(
292+
'Expected first argument to doc() to be a CollectionReference, a DocumentReference or FirebaseFirestore'
293+
);
288294
expect(() => doc(db, 'coll')).to.throw(
289295
'Invalid document reference. Document references must have an even ' +
290296
'number of segments, but coll has 1.'

0 commit comments

Comments
 (0)