Skip to content

Commit 67c5a90

Browse files
authored
Support converter that returns undefined. (#7738)
* Support converter that returns undefined. * Changeset * Format
1 parent 12ad9f1 commit 67c5a90

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

.changeset/strong-coins-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/firestore-compat': patch
3+
---
4+
5+
Allow converter return value of undefined.

packages/firestore-compat/src/api/database.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,11 +972,16 @@ export class QueryDocumentSnapshot<T = PublicDocumentData>
972972
{
973973
data(options?: PublicSnapshotOptions): T {
974974
const data = this._delegate.data(options);
975-
_debugAssert(
976-
data !== undefined,
977-
'Document in a QueryDocumentSnapshot should exist'
978-
);
979-
return data;
975+
if (this._delegate._converter) {
976+
// Undefined is a possible valid value from converter.
977+
return data as T;
978+
} else {
979+
_debugAssert(
980+
data !== undefined,
981+
'Document in a QueryDocumentSnapshot should exist'
982+
);
983+
return data;
984+
}
980985
}
981986
}
982987

packages/firestore-compat/test/database.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,30 @@ apiDescribe('Database', (persistence: boolean) => {
16471647
expect(untypedDocRef.isEqual(ref)).to.be.true;
16481648
});
16491649
});
1650+
1651+
it('for DocumentReference.withConverter() that returns undefined', () => {
1652+
return withTestDb(persistence, async db => {
1653+
const docRef = db
1654+
.collection('posts')
1655+
.doc()
1656+
.withConverter({
1657+
toFirestore(post: Post): firestore.DocumentData {
1658+
return { title: post.title, author: post.author };
1659+
},
1660+
fromFirestore(
1661+
snapshot: firestore.QueryDocumentSnapshot,
1662+
options: firestore.SnapshotOptions
1663+
): Post | undefined {
1664+
return undefined;
1665+
}
1666+
});
1667+
1668+
await docRef.set(new Post('post', 'author'));
1669+
const postData = await docRef.get();
1670+
const post = postData.data();
1671+
expect(post).to.equal(undefined);
1672+
});
1673+
});
16501674
});
16511675

16521676
// TODO(b/196858864): This test regularly times out on CI.

0 commit comments

Comments
 (0)