【发布时间】:2021-06-26 01:17:27
【问题描述】:
我正在尝试让 lesson.contents 在屏幕上呈现,但我在控制台上出现了一些错误。这是代码(我在 Vue 3 中使用 Firebase)
<template>
<AddContent :lesson="lesson" />
<div v-if="lesson.contents.length">
<h2>{{ lesson.contents }}</h2>
</div>
</template>
<script>
import ContentList from "@/components/ContentList.vue";
import AddContent from "@/components/AddContent.vue";
import getDocument from "@/composables/getDocument";
import { ref } from "vue";
export default {
props: ["id"],
components: { AddContent },
setup(props) {
const { document: lesson } = getDocument("lessons", props.id);
return { lesson };
},
};
</script>
<style>
</style>
然后我有这个错误:
我感到困惑的是,我仍然能够在屏幕上呈现 lesson.contents:
我已经尝试了几个小时来修复它,但我找不到原因。我真的很感谢你的帮助。谢谢!
我的getDocument.js 代码:
import { watchEffect, ref } from 'vue'
import { projectFirestore } from '../firebase/config'
const getDocument = (collection, id) => {
let document = ref(null)
let error = ref(null)
// register the firestore collection reference
let documentRef = projectFirestore.collection(collection).doc(id)
const unsub = documentRef.onSnapshot(doc => {
// need to make sure the doc exists & has data
if(doc.data()) {
document.value = {...doc.data(), id: doc.id}
error.value = null
}
else {
error.value = 'that document does not exist'
}
}, err => {
console.log(err.message)
error.value = 'problem fetching the document'
})
watchEffect((onInvalidate) => {
onInvalidate(() => unsub())
});
return { error, document }
}
export default getDocument
【问题讨论】:
-
看起来
getDocument()应该返回一个初始对象,其中contents设置为一个空数组。现在,它最初似乎没有contents属性 -
谢谢。你会建议我做什么来解决它?我使用 Vue 调试器检查了 props,它显示了
contents属性的数据。你可以在这里查看截图:screencast.com/t/z7TQt1pM -
请在您的问题中包含
getDocument实现。 -
This post 可能有助于说明我所说的“初始数据”
-
我刚刚为
getDocument.js添加了我的代码。你能帮忙看看菲尔我能做什么吗?谢谢!
标签: javascript firebase vue.js google-cloud-firestore vuejs3