【发布时间】:2018-09-05 08:06:33
【问题描述】:
我在一个应用程序中使用 Vue js 和 Firebase。我创建了一个检索每月费用的函数。
totalExpenseByType(){
db.collection('expenses').where("created_month", "==", moment().format('MM-YYYY'))
.get()
.then(snapshot => {
var totalExpensesOfThisMonth = 0;
snapshot.forEach(doc => {
totalExpensesOfThisMonth += Number(doc.data().expense_amount)
})
this.expenses_of_this_month = totalExpensesOfThisMonth;
})
return this.expenses_of_this_month;
}
效果很好。
但是我添加了一个参数 where 条件。它的行为很奇怪。它返回不可停止的循环,重复所有数据。
totalExpenseByType(expense_type){
db.collection('expenses').where("created_month", "==", moment().format('MM-YYYY'))
.where("expense_type", "==", {"expense_type": expense_type})
.get()
.then(snapshot => {
var totalExpensesOfThisMonth = 0;
snapshot.forEach(doc => {
totalExpensesOfThisMonth += Number(doc.data().expense_amount)
})
this.expenses_of_this_month = totalExpensesOfThisMonth;
})
return this.expenses_of_this_month;
}
这是我用于显示数据的代码
<template slot="items" slot-scope="props">
<td class="text-xs-left">{{ props.item.expense_type }}</td>
<td class="text-xs-left">{{ totalExpenseByType(props.item.expense_type) }}</td>
<v-btn fab dark small color="pink" @click="removeExepenseType(props.item.id)">
<v-icon dark>remove</v-icon>
</v-btn>
</template>
数据结构
created_month: "09-2018"
expense_amount: "600"
expense_title:"Employee Salary"
expense_type:"Employee Expense"
timestamp:1536126964353
【问题讨论】:
标签: javascript loops firebase vue.js firebase-realtime-database