Javascript Firebase中从开始日期到结束日期的项目总数
我正试图得到从开始日期到结束日期的费用总和Javascript Firebase中从开始日期到结束日期的项目总数,javascript,firebase,firebase-realtime-database,ionic3,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.subscrib
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))
}
我想从这些日期范围中获取支出总额。以下是Firebase DB结构屏幕截图
任何帮助都将是巨大的在您的服务中进行查询。ts
getExpenseValue(monthStartDate, monthEndDate): AngularFireList<any>{
return this.afDatabase.list('/userProfile/${this.userId}/expenseList' ,
ref => ref.startAt(monthStartDate).endAt(monthEndDate))
}
您是否获得了所需的数据?我认为您需要查询
列表
,而不是在订阅中进行查询。你能显示你的.getExpenseValue()
代码吗?getExpenseValue():AngularFireList{返回这个.afDatabase.list(
/userProfile/${this.userId}/expenseList)}
我在console.log(this.totalExpense)
doconsole.log(a.payload.val())上获得值0
insideactions.map
并检查您的查询返回数据,或者在html{{monthExpense | async | json}}
我的错误我以为您在查询expense
节点。因此,您可以在expenses
下获得包含多个费用的每份单据。您需要执行foreach来获取值。答案仍然是0
或null
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;
})
});