我想从开始日期到结束日期获得费用总和。
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数据库结构截图
任何帮助都应该很棒
答案 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;
})
});