Skip to content

Commit 3d0b326

Browse files
committed
fix(core): drop raw refs when updating docs
Closes vuejs#831
1 parent 41a9298 commit 3d0b326

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('refs in documents', () => {
8080
expect(vm.arr).toEqual({ refs: [b.path, c.path] })
8181
})
8282

83+
it('keeps correct elements in array of references when removing in the middle', async () => {
84+
const arr = collection.doc()
85+
vm.arr = null
86+
87+
await arr.update({ refs: [a, b, c] })
88+
await bind('arr', arr, { maxRefDepth: 0 })
89+
await arr.update({ refs: [a, c] })
90+
91+
expect(vm.arr).toEqual({ refs: [a.path, c.path] })
92+
})
93+
8394
it('binds refs on documents', async () => {
8495
// create an empty doc and update using the ref instead of plain data
8596
await item.update({ ref: c })
@@ -124,7 +135,7 @@ describe('refs in documents', () => {
124135
})
125136
})
126137

127-
it.only('does not lose empty references in arrays of objects when updating a property', async () => {
138+
it('does not lose empty references in arrays of objects when updating a property', async () => {
128139
const emptyItem = collection.doc()
129140
await item.update({ todos: [{ ref: emptyItem }], toggle: true })
130141
await bind('item', item)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ export function extractRefs(
6464
} else if (isDocumentRef(ref)) {
6565
// allow values to be null (like non-existant refs)
6666
// TODO: better typing since this isObject shouldn't be necessary but it doesn't work
67-
data[key] = typeof oldDoc === 'object' && key in oldDoc ? oldDoc[key] : ref.path
67+
data[key] =
68+
typeof oldDoc === 'object' &&
69+
key in oldDoc &&
70+
// only copy refs if they were refs before
71+
// https://github.com/vuejs/vuefire/issues/831
72+
typeof oldDoc[key] != 'string'
73+
? oldDoc[key]
74+
: ref.path
6875
// TODO: handle subpathes?
6976
refs[path + key] = ref
7077
} else if (Array.isArray(ref)) {

0 commit comments

Comments
 (0)