Skip to content

Commit 7f6da0b

Browse files
author
Ben Delaney
authored
fix(firestore): iOS nested FieldValues not serialized in update() (#139)
* fix(firestore): iOS nested FieldValues not serialized in update() * Fix top level FieldValue values become `{}`
1 parent 47170af commit 7f6da0b

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

packages/firebase-firestore/index.ios.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,19 @@ function serializeItems(value) {
140140
return value;
141141
}
142142

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]));
151151
} 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);
162153
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));
168156
}
169157
}
170158

@@ -202,7 +190,6 @@ export class Transaction implements ITransaction {
202190
update<T extends DocumentData = DocumentData, K extends keyof T = string>(documentRef: DocumentReference<T>, field: K | FieldPath, value: T[K], moreFieldsAndValues: any[]): Transaction;
203191
update(documentRef: any, field: any, value?: any, moreFieldsAndValues?: any): Transaction {
204192
const data = createDictionary(field, value, moreFieldsAndValues);
205-
206193
return Transaction.fromNative(this._native.updateDataForDocument(data as any, documentRef?.native));
207194
}
208195

0 commit comments

Comments
 (0)