Skip to content

Commit e81c429

Browse files
author
Brian Chen
authored
Call toFirestore() only once (#3755)
1 parent 35d1c0d commit e81c429

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

.changeset/tame-donuts-relate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/firestore': patch
3+
---
4+
5+
Fixed a bug where CollectionReference.add() called FirestoreDataConverter.toFirestore() twice intead of once (#3742).

packages/firestore/lite/src/api/field_value.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import { ParseContext } from '../../../src/api/user_data_reader';
3030
import { FieldTransform } from '../../../src/model/mutation';
3131

3232
/** The public FieldValue class of the lite API. */
33-
export abstract class FieldValue extends SerializableFieldValue
33+
export abstract class FieldValue
34+
extends SerializableFieldValue
3435
implements firestore.FieldValue {}
3536

3637
/**

packages/firestore/src/api/database.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2379,8 +2379,17 @@ export class CollectionReference<T = DocumentData>
23792379
? this._converter.toFirestore(value)
23802380
: value;
23812381
validateArgType('CollectionReference.add', 'object', 1, convertedValue);
2382+
23822383
const docRef = this.doc();
2383-
return docRef.set(value).then(() => docRef);
2384+
2385+
// Call set() with the converted value directly to avoid calling toFirestore() a second time.
2386+
return new DocumentReference(
2387+
(docRef as DocumentReference<T>)._key,
2388+
this.firestore,
2389+
null
2390+
)
2391+
.set(convertedValue)
2392+
.then(() => docRef);
23842393
}
23852394

23862395
withConverter<U>(

0 commit comments

Comments
 (0)