Skip to content

Commit b50ac72

Browse files
committed
fix(firestore): array with objects with references
Fix vuejs#576
1 parent f23540e commit b50ac72

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

packages/@posva/vuefire-core/__tests__/firestore/refs-documents.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ describe('refs in documents', () => {
124124
})
125125
})
126126

127+
it.only('does not lose empty references in arrays of objects when updating a property', async () => {
128+
const emptyItem = collection.doc()
129+
await item.update({ todos: [{ ref: emptyItem }], toggle: true })
130+
await bind('item', item)
131+
expect(vm.item).toEqual({
132+
todos: [{ ref: null }],
133+
toggle: true,
134+
})
135+
await item.update({ toggle: false })
136+
expect(vm.item).toEqual({
137+
todos: [{ ref: null }],
138+
toggle: false,
139+
})
140+
})
141+
127142
it('keeps array of references when updating a property', async () => {
128143
await item.update({ a: [a, b, c, { foo: 'bar' }], toggle: true })
129144
await bind('item', item)

packages/@posva/vuefire-core/src/firestore/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createSnapshot, extractRefs, FirestoreSerializer, FirestoreReference } from './utils'
1+
import { createSnapshot, extractRefs, FirestoreSerializer } from './utils'
22
import { walkGet, callOnceWithArg, OperationsType } from '../shared'
33
import { firestore } from 'firebase'
44

packages/@posva/vuefire-core/src/firestore/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ export function extractRefs(
7272
// fill existing refs into data but leave the rest empty
7373
for (let i = 0; i < ref.length; i++) {
7474
const newRef = ref[i]
75+
// TODO: this only works with array of primitives but not with nested properties like objects with References
7576
if (newRef.path in subsByPath) data[key][i] = subsByPath[newRef.path]
7677
}
77-
// the oldArray is in this case the same array with holes
78-
recursiveExtract(ref, data[key], path + key + '.', [data[key], refs])
78+
// the oldArray is in this case the same array with holes unless the array already existed
79+
recursiveExtract(ref, oldDoc[key] || data[key], path + key + '.', [data[key], refs])
7980
} else if (isObject(ref)) {
8081
data[key] = {}
8182
recursiveExtract(ref, oldDoc[key], path + key + '.', [data[key], refs])

0 commit comments

Comments
 (0)