Javascript 函数参数在循环内不工作
我在应用程序中使用Vue js和Firebase。我创建了一个用于检索每月费用的函数Javascript 函数参数在循环内不工作,javascript,loops,firebase,vue.js,firebase-realtime-database,Javascript,Loops,Firebase,Vue.js,Firebase Realtime Database,我在应用程序中使用Vue js和Firebase。我创建了一个用于检索每月费用的函数 totalExpenseByType(){ db.collection('expenses').where("created_month", "==", moment().format('MM-YYYY')) .get() .then(snapshot => { var totalExpensesOfThisMonth = 0;
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>
更新以下是关于数据结构的最新更新…:
按照我最初的建议去做:
totalExpenseByType(expense_type){
db.collection('expenses')
.where("created_month", "==", moment().format('MM-YYYY'))
.where("expense_type", "==", expense_type)
.get()
.....
那么,你还有另一个问题。get()
方法是异步的,返回一个承诺,请参阅。通过做
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;
}
在承诺解决之前,您正在返回本月的this.expenses\u的值,因此它不会返回正确的值
此外,我认为您误解了Vue的反应系统:在您的代码中,您(如果我假设this
是您的Vue实例是正确的话)将totalexpensesofthis month
的值分配给您组件的数据对象的expenses\u,并返回,在您的totalExpenseByType(费用类型)
函数中,相同的数据
对象属性(错误,请参见上文)
根据您在下面的最后一条评论进行更新:是一个值为“expense\u type:“Employee expense”的字符串
既然您提到它是一个值为“expense_type:“Employee expense”的字符串,那么您应该执行以下操作:
db.collection("test").where("expense_type", "==", '"expense_type:"Employee Expense"').get().then()
db.collection('expenses').where("expense_type.expense_type", "==", "Employee Expense").get().then()
totalExpenseByType(expense_type){
db.collection('expenses')
.where("created_month", "==", moment().format('MM-YYYY'))
.where("expense_type", "==", expense_type)
.get()
.....
或者,如果要删除外部双引号:
db.collection("test").where("expense_type", "==", 'expense_type:"Employee Expense').get().then()
更新以下有关数据结构的更新:
如果我理解正确,您正在将费用类型
存储为对象
在这种情况下,您的查询应如下所示:
db.collection("test").where("expense_type", "==", '"expense_type:"Employee Expense"').get().then()
db.collection('expenses').where("expense_type.expense_type", "==", "Employee Expense").get().then()
totalExpenseByType(expense_type){
db.collection('expenses')
.where("created_month", "==", moment().format('MM-YYYY'))
.where("expense_type", "==", expense_type)
.get()
.....
先前的答案,将被丢弃
我认为应该是这样的:
db.collection("test").where("expense_type", "==", '"expense_type:"Employee Expense"').get().then()
db.collection('expenses').where("expense_type.expense_type", "==", "Employee Expense").get().then()
totalExpenseByType(expense_type){
db.collection('expenses')
.where("created_month", "==", moment().format('MM-YYYY'))
.where("expense_type", "==", expense_type)
.get()
.....
除非费用类型
字段为数组类型。在这种情况下,您应该使用“数组成员资格”语法,请参见no这不是问题所在。我必须这样使用{“expense\u type”:expense\u type}因为数据结构是这样的,您可以通过将数据结构添加到原始问题中来共享您的数据结构吗?或者它是一个值为“expense\u type:“Employee expense”的字符串?是一个值为“expense\u type:“Employee expense”的字符串,然后执行db.collection(“test”)。其中(“expense\u type”,“==”,““expense\u type:“Employee expense”)。get()。然后()
。我将更新我的答案。