Skip to content

fix(firestore): iOS nested FieldValues not serialized in update() #139

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
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
35 changes: 11 additions & 24 deletions packages/firebase-firestore/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,19 @@ function serializeItems(value) {
return value;
}

function createDictionary(field: any, value?: any, moreFieldsAndValues?: any) {
const data = {};
if (data && !value && !moreFieldsAndValues) {
Object.entries(field).forEach((item) => {
const key = <any>item[0];
const value = <any>item[1];
if (key instanceof FieldPath) {
data[key.native as any] = value?.native || value;
function createDictionary(dataOrField: any, fieldPathValue?: any, moreFieldsAndValues?: any) {
// TODO: Correctly handle FieldPaths used as keys or passed in the dataOrField and
// moreFieldsAndValues params e.g. new FieldPath(['test', '123']) will currently result in an
// object like { "<FIRFieldPath: 0x283f94640>": "value" }
const data = {}
const assignKeyValue = (key, val) => data[key instanceof FieldPath ? key.native : key] = serializeItems(val);
if (!fieldPathValue && !moreFieldsAndValues) {
Object.entries(dataOrField).forEach((item) => assignKeyValue(item[0], item[1]));
} else {
data[key] = value?.native || value;
}
});
} else {
if (field instanceof FieldPath) {
data[field.native as any] = value?.native || value;
} else {
data[field] = value?.native || value;
}

assignKeyValue(dataOrField, fieldPathValue);
if (Array.isArray(moreFieldsAndValues)) {
for (let i = 0; i < moreFieldsAndValues.length; i += 2) {
const key = moreFieldsAndValues[i];
const value = moreFieldsAndValues[i + 1];
data[key?.native || key] = value?.native || value;
}
Object.entries(Object.fromEntries(moreFieldsAndValues))
.forEach(([key, value]) => assignKeyValue(key, value));
}
}

Expand Down Expand Up @@ -202,7 +190,6 @@ export class Transaction implements ITransaction {
update<T extends DocumentData = DocumentData, K extends keyof T = string>(documentRef: DocumentReference<T>, field: K | FieldPath, value: T[K], moreFieldsAndValues: any[]): Transaction;
update(documentRef: any, field: any, value?: any, moreFieldsAndValues?: any): Transaction {
const data = createDictionary(field, value, moreFieldsAndValues);

return Transaction.fromNative(this._native.updateDataForDocument(data as any, documentRef?.native));
}

Expand Down