【发布时间】:2020-10-12 12:46:04
【问题描述】:
我正在尝试从 Cloud Firestore 数据库中检索数据。 但是我遇到了一个错误,
[Vue 警告]:渲染错误:“TypeError:product.data 不是 函数”
我想在我的表格中显示每个产品的名称和价格。
但我不知道为什么会出现这个问题。 所以我希望有人可以帮助我。
如果我不使用vue模板中的data(),我可以看到所有我期望的数据。
<template>
<h3>Product List</h3>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
<tr v-for="(product, index) in products" :key="index">
<td>{{ product.data().name }}</td>
<td>{{ product.data().price }}</td>
<td>
<button @click="editProduct(product)" class="btn btn-primary">Edit</button>
<button @click="deleteProduct(product.id)" class="btn btn-danger">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { fb, db } from '../firebase'
export default {
name: 'Products',
props: {
msg: String
},
data () {
return {
products: [],
product: {//object
name: null,
price: null
}
}
},
methods: {
editProduct(product) {
$('#edit').modal('show')
},
readData() {
db.collection("products").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
this.products.push(doc.data());
});
});
},
saveData() {
// Add a new document with a generated id.
db.collection("products").add(this.product)
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
this.product.name = "",
this.product.price = ""
this.readData()
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
},
created() {
this.readData();
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
【问题讨论】:
-
你在使用 jQuery + Vue 吗?
$('#edit').modal('show'),好吧,这不是一个好方法。无论如何,如果您将 doc.data 推送到您的 products 数组,这意味着您正在将数据对象传递给它,所以我想您只需将product.data().name更改为product.name,这是基于您的代码的假设。
标签: firebase vue.js google-cloud-firestore vue-component information-retrieval