Skip to content

Allow Collection.add() to use custom objects #2607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2020
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
4 changes: 4 additions & 0 deletions packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Unreleased
- [fixed] Fixed an issue where `CollectionReference.add()` would reject
custom types when using `withConverter()`. (#2606)

# 1.9.3
- [fixed] Fixed an issue where auth credentials were not respected in some
Firefox or Chrome extensions. (#1491)
- [changed] Firestore previously required that every document read in a
Expand Down
5 changes: 4 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,10 @@ export class CollectionReference<T = firestore.DocumentData> extends Query<T>

add(value: T): Promise<firestore.DocumentReference<T>> {
validateExactNumberOfArgs('CollectionReference.add', arguments, 1);
validateArgType('CollectionReference.add', 'object', 1, value);
const convertedValue = this._converter
? this._converter.toFirestore(value)
: value;
validateArgType('CollectionReference.add', 'object', 1, convertedValue);
const docRef = this.doc();
return docRef.set(value).then(() => docRef);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,12 +1214,9 @@ apiDescribe('Database', (persistence: boolean) => {

it('for CollectionReference.withConverter()', () => {
return withTestDb(persistence, async db => {
const docRef = db
.collection('posts')
.withConverter(postConverter)
.doc();
const coll = db.collection('posts').withConverter(postConverter);

await docRef.set(new Post('post', 'author'));
const docRef = await coll.add(new Post('post', 'author'));
const postData = await docRef.get();
const post = postData.data();
expect(post).to.not.equal(undefined);
Expand Down