Firebase中从开始日期到结束日期的项目总和

时间:2018-02-02 04:32:40

标签: javascript firebase firebase-realtime-database ionic3

我想从开始日期到结束日期获得费用总和。

monthStartDate = moment().startOf('month').format('YYYY-MM-DD');
monthEndDate = moment().endOf('month').format('YYYY-MM-DD');

this.monthExpense = this.expenseProvider.getExpenseValue().valueChanges();
this.monthExpense.subscribe(ref =>
  ref.startAt(this.monthStartDate).endAt(this.monthEndDate))    
}

我想从这些日期范围中获取expenseAmount的总数。以下是Firebase数据库结构截图

firebase_database

任何帮助都应该很棒

1 个答案:

答案 0 :(得分:1)

在service.ts中查询

getExpenseValue(monthStartDate, monthEndDate): AngularFireList<any>{ 
    return this.afDatabase.list('/userProfile/${this.userId}/expenseList' ,
        ref => ref.startAt(monthStartDate).endAt(monthEndDate)) 
}

在你的component.ts

totalExpense:number; //声明

monthStartDate = moment().startOf('month').format('YYYY-MM-DD');
monthEndDate = moment().endOf('month').format('YYYY-MM-DD');

this.totalExpense = 0;//reset
this.monthExpense = this.expenseProvider.getExpenseValue(this.monthStartDate,this.monthEndDate);
this.monthExpense.snapshotChanges()
  .map(actions => {
    return actions.map(a=>{
        a.payload.val().expense.forEach(res=>{
            this.totalExpense += res.expenseAmount;
        }
        return this.totalExpense;
    })
  });