Skip to content

Commit

Permalink
Call toFirestore() only once (#3755)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Chen committed Sep 11, 2020
1 parent 35d1c0d commit e81c429
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-donuts-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': patch
---

Fixed a bug where CollectionReference.add() called FirestoreDataConverter.toFirestore() twice intead of once (#3742).
3 changes: 2 additions & 1 deletion packages/firestore/lite/src/api/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { ParseContext } from '../../../src/api/user_data_reader';
import { FieldTransform } from '../../../src/model/mutation';

/** The public FieldValue class of the lite API. */
export abstract class FieldValue extends SerializableFieldValue
export abstract class FieldValue
extends SerializableFieldValue
implements firestore.FieldValue {}

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2379,8 +2379,17 @@ export class CollectionReference<T = DocumentData>
? this._converter.toFirestore(value)
: value;
validateArgType('CollectionReference.add', 'object', 1, convertedValue);

const docRef = this.doc();
return docRef.set(value).then(() => docRef);

// Call set() with the converted value directly to avoid calling toFirestore() a second time.
return new DocumentReference(
(docRef as DocumentReference<T>)._key,
this.firestore,
null
)
.set(convertedValue)
.then(() => docRef);
}

withConverter<U>(
Expand Down

0 comments on commit e81c429

Please sign in to comment.