diff --git a/.changeset/strong-coins-repeat.md b/.changeset/strong-coins-repeat.md new file mode 100644 index 00000000000..f9a52b827d5 --- /dev/null +++ b/.changeset/strong-coins-repeat.md @@ -0,0 +1,5 @@ +--- +'@firebase/firestore-compat': patch +--- + +Allow converter return value of undefined. diff --git a/packages/firestore-compat/src/api/database.ts b/packages/firestore-compat/src/api/database.ts index f4e63c09d77..e7cbe8196d6 100644 --- a/packages/firestore-compat/src/api/database.ts +++ b/packages/firestore-compat/src/api/database.ts @@ -972,11 +972,16 @@ export class QueryDocumentSnapshot { data(options?: PublicSnapshotOptions): T { const data = this._delegate.data(options); - _debugAssert( - data !== undefined, - 'Document in a QueryDocumentSnapshot should exist' - ); - return data; + if (this._delegate._converter) { + // Undefined is a possible valid value from converter. + return data as T; + } else { + _debugAssert( + data !== undefined, + 'Document in a QueryDocumentSnapshot should exist' + ); + return data; + } } } diff --git a/packages/firestore-compat/test/database.test.ts b/packages/firestore-compat/test/database.test.ts index 69237d74897..00612972c0c 100644 --- a/packages/firestore-compat/test/database.test.ts +++ b/packages/firestore-compat/test/database.test.ts @@ -1647,6 +1647,30 @@ apiDescribe('Database', (persistence: boolean) => { expect(untypedDocRef.isEqual(ref)).to.be.true; }); }); + + it('for DocumentReference.withConverter() that returns undefined', () => { + return withTestDb(persistence, async db => { + const docRef = db + .collection('posts') + .doc() + .withConverter({ + toFirestore(post: Post): firestore.DocumentData { + return { title: post.title, author: post.author }; + }, + fromFirestore( + snapshot: firestore.QueryDocumentSnapshot, + options: firestore.SnapshotOptions + ): Post | undefined { + return undefined; + } + }); + + await docRef.set(new Post('post', 'author')); + const postData = await docRef.get(); + const post = postData.data(); + expect(post).to.equal(undefined); + }); + }); }); // TODO(b/196858864): This test regularly times out on CI.