Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/strong-coins-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore-compat': patch
---

Allow converter return value of undefined.
15 changes: 10 additions & 5 deletions packages/firestore-compat/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,16 @@ export class QueryDocumentSnapshot<T = PublicDocumentData>
{
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;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/firestore-compat/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down