I'm trying to get a single record from Firestore to display in Vue.js. I'm using VueFire and the guide here.
<script setup>
import { initializeApp } from 'firebase/app'
import { getFirestore , doc } from "firebase/firestore";
import { useDocument } from 'vuefire'
const firebaseConfig = {...};
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);
const analytics = getAnalytics(firebaseApp);
const db = getFirestore(firebaseApp);
const place = useDocument(doc(db, "data", "key"));
console.log(place)
</script>
<template>
{{ place.title }}
</template>
The data logged is RefImpl {__v_isShallow: false, dep: undefined, __v_isRef: true, _rawValue : {title: 'I am a title',
however when it gets to the template there is an error
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'title')
Maybe try this.
I'm just following what they the author of VueFire has posted here: https://github.com/vuejs/vuefire/blob/df3c235f226d4e4c821391bcce74a1c3a6134406/packages/nuxt/playground/pages/firestore-useDocument.vue
You can use the
pending
property to show the place once it has loaded.You can also read about Subscription State here where they talk about destructuring. https://v3.vuefire.vuejs.org/guide/realtime-data.html#subscription-state