persisting a reference in a collection #1396
-
hey, im struggeling to properly persist a reference inside a document. assume this collections: user:
xyz: {name: "Foo"}
abc: {name: "Bar"}
entity:
123:
name: "123"
users:
- users/xyz
- users/abc now im trying to create a new entity: const coll = collection("entity");
const userColl = collection("user");
const p = { name: "Foobar", users: [
doc(userColl, "xyz"),
doc(userColl, "abc"),
]}
await addDoc(coll, p); but somehow its not working as expect, its not creating a clean reference like so how do i create references properly? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
They are probably being stored correctly but just being rendered as their original values by VueFire: https://vuefire.vuejs.org/guide/realtime-data.html#references-firestore-only |
Beta Was this translation helpful? Give feedback.
-
No, the are not. I can see that in the firestore ui. Any other ideas?
Eduardo San Martin Morote ***@***.***> schrieb am Do., 27.
Juli 2023, 23:22:
… They are probably being stored correctly but just being rendered as their
original values by VueFire:
https://vuefire.vuejs.org/guide/realtime-data.html#references-firestore-only
—
Reply to this email directly, view it on GitHub
<#1396 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACHVVZTQPCE4AS6U5ANA2TXSLL2XANCNFSM6AAAAAA2TDIZHQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
i wrote a vitest unit test, and after some fiddeling around it seems to work as expected (i have to rely on it("lifecycle", async () => {
const coll = collection(firestore, "test_entity");
const userColl = collection(firestore, "test_user");
// setup
const userA = await addDoc(userColl, {
name: "User A"
});
const userB = await addDoc(userColl, {
name: "User B"
});
// create entity
const result = await addDoc(coll, {
name: "Foobar",
users: [
doc(userColl, userA.id),
]});
expect(result.id).toBeTruthy()
// fetch and check after creation
const {
data: updatedEntity,
promise,
} = useDocument(doc(coll, result.id))
await promise.value
expect(updatedEntity.value.users).toHaveLength(1)
expect(updatedEntity.value.users[0].name).toEqual("User A")
expect(updatedEntity.value.users[0].id).toEqual(userA.id)
// update
await setDoc(doc(coll, updatedEntity.value.id), {
users: [
doc(userColl, userB.id),
]
});
// fetch and check after update
const {
data: updatedEntity2,
promise: promise2,
} = useDocument(doc(coll, result.id))
await promise2.value
expect(updatedEntity2.value.users).toHaveLength(1)
expect(updatedEntity2.value.users[0].name).toEqual("User B")
expect(updatedEntity2.value.users[0].id).toEqual(userB.id)
}) |
Beta Was this translation helpful? Give feedback.
i wrote a vitest unit test, and after some fiddeling around it seems to work as expected (i have to rely on
useDocument
and their promises properly)