Skip to content

Commit eb614f0

Browse files
author
Brian Chen
authored
Allow Collection.add() to use custom objects (#2607)
1 parent 50134d6 commit eb614f0

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

packages/firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Unreleased
2+
- [fixed] Fixed an issue where `CollectionReference.add()` would reject
3+
custom types when using `withConverter()`. (#2606)
4+
5+
# 1.9.3
26
- [fixed] Fixed an issue where auth credentials were not respected in some
37
Firefox or Chrome extensions. (#1491)
48
- [changed] Firestore previously required that every document read in a

packages/firestore/src/api/database.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,10 @@ export class CollectionReference<T = firestore.DocumentData> extends Query<T>
24612461

24622462
add(value: T): Promise<firestore.DocumentReference<T>> {
24632463
validateExactNumberOfArgs('CollectionReference.add', arguments, 1);
2464-
validateArgType('CollectionReference.add', 'object', 1, value);
2464+
const convertedValue = this._converter
2465+
? this._converter.toFirestore(value)
2466+
: value;
2467+
validateArgType('CollectionReference.add', 'object', 1, convertedValue);
24652468
const docRef = this.doc();
24662469
return docRef.set(value).then(() => docRef);
24672470
}

packages/firestore/test/integration/api/database.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,12 +1214,9 @@ apiDescribe('Database', (persistence: boolean) => {
12141214

12151215
it('for CollectionReference.withConverter()', () => {
12161216
return withTestDb(persistence, async db => {
1217-
const docRef = db
1218-
.collection('posts')
1219-
.withConverter(postConverter)
1220-
.doc();
1217+
const coll = db.collection('posts').withConverter(postConverter);
12211218

1222-
await docRef.set(new Post('post', 'author'));
1219+
const docRef = await coll.add(new Post('post', 'author'));
12231220
const postData = await docRef.get();
12241221
const post = postData.data();
12251222
expect(post).to.not.equal(undefined);

0 commit comments

Comments
 (0)