@@ -140,31 +140,19 @@ function serializeItems(value) {
140
140
return value ;
141
141
}
142
142
143
- function createDictionary ( field : any , value ?: any , moreFieldsAndValues ?: any ) {
144
- const data = { } ;
145
- if ( data && ! value && ! moreFieldsAndValues ) {
146
- Object . entries ( field ) . forEach ( ( item ) => {
147
- const key = < any > item [ 0 ] ;
148
- const value = < any > item [ 1 ] ;
149
- if ( key instanceof FieldPath ) {
150
- data [ key . native as any ] = value ?. native || value ;
143
+ function createDictionary ( dataOrField : any , fieldPathValue ?: any , moreFieldsAndValues ?: any ) {
144
+ // TODO: Correctly handle FieldPaths used as keys or passed in the dataOrField and
145
+ // moreFieldsAndValues params e.g. new FieldPath(['test', '123']) will currently result in an
146
+ // object like { "<FIRFieldPath: 0x283f94640>": "value" }
147
+ const data = { }
148
+ const assignKeyValue = ( key , val ) => data [ key instanceof FieldPath ? key . native : key ] = serializeItems ( val ) ;
149
+ if ( ! fieldPathValue && ! moreFieldsAndValues ) {
150
+ Object . entries ( dataOrField ) . forEach ( ( item ) => assignKeyValue ( item [ 0 ] , item [ 1 ] ) ) ;
151
151
} else {
152
- data [ key ] = value ?. native || value ;
153
- }
154
- } ) ;
155
- } else {
156
- if ( field instanceof FieldPath ) {
157
- data [ field . native as any ] = value ?. native || value ;
158
- } else {
159
- data [ field ] = value ?. native || value ;
160
- }
161
-
152
+ assignKeyValue ( dataOrField , fieldPathValue ) ;
162
153
if ( Array . isArray ( moreFieldsAndValues ) ) {
163
- for ( let i = 0 ; i < moreFieldsAndValues . length ; i += 2 ) {
164
- const key = moreFieldsAndValues [ i ] ;
165
- const value = moreFieldsAndValues [ i + 1 ] ;
166
- data [ key ?. native || key ] = value ?. native || value ;
167
- }
154
+ Object . entries ( Object . fromEntries ( moreFieldsAndValues ) )
155
+ . forEach ( ( [ key , value ] ) => assignKeyValue ( key , value ) ) ;
168
156
}
169
157
}
170
158
@@ -202,7 +190,6 @@ export class Transaction implements ITransaction {
202
190
update < T extends DocumentData = DocumentData , K extends keyof T = string > ( documentRef : DocumentReference < T > , field : K | FieldPath , value : T [ K ] , moreFieldsAndValues : any [ ] ) : Transaction ;
203
191
update ( documentRef : any , field : any , value ?: any , moreFieldsAndValues ?: any ) : Transaction {
204
192
const data = createDictionary ( field , value , moreFieldsAndValues ) ;
205
-
206
193
return Transaction . fromNative ( this . _native . updateDataForDocument ( data as any , documentRef ?. native ) ) ;
207
194
}
208
195
0 commit comments