我在应用程序中使用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

最佳答案

按照有关数据结构的最新更新进行更新...:

按照我最初的建议做:

totalExpenseByType(expense_type){
       db.collection('expenses')
       .where("created_month", "==", moment().format('MM-YYYY'))
       .where("expense_type", "==", expense_type)
       .get()
        .....


然后,您还有另一个问题。 get()方法是异步的,并返回一个Promise,请参见here。通过做

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_of_this_month的值,因此它没有返回正确的值。

另外,我认为您对Vue的反应性系统有误解:在您的代码中,您(如果我正确假设this是您的Vue实例,则)将totalExpensesOfThisMonth的值分配给组件的expenses_of_this_month属性cc>对象,并在您的data函数中返回相同的totalExpenseByType(expense_type)对象属性(不正确,请参见上文)。



在下面的最后一条评论之后进行更新:是一个值为“ expense_type:” Employee Expense”的字符串

由于您提到它是一个值为“ expense_type:” Employee Expense“的字符串,因此您应该执行以下操作:

db.collection("test").where("expense_type", "==", '"expense_type:"Employee Expense"').get().then()


或者,如果要删除外部双引号:

db.collection("test").where("expense_type", "==", 'expense_type:"Employee Expense').get().then()




在有关数据结构的更新之后进行更新:

如果我理解正确,则您将data存储为对象。

在这种情况下,您的查询应如下所示:

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()
    .....


除非expense_type字段的类型为Array。在这种情况下,您应该使用“数组成员”语法,请参见https://firebase.google.com/docs/firestore/query-data/queries#array_membership

07-24 09:47
查看更多