从angularfire数据库列表中返回的值订阅

时间:2018-08-14 00:44:34

标签: javascript angular firebase firebase-realtime-database

我试图将“费用”列表中每笔交易的“金额”值相加。

这是我的数据库:

enter image description here

订阅中的日志有效,总计1700,但是我无法从函数中获取“ this.expenseListTotal”的值以在其他组件中显示。如何返回“ this.expenseListTotal”值,以便可以在其他组件中使用它?

code . Filename.cs

1 个答案:

答案 0 :(得分:0)

服务中:

getExpenseListTotals(budgetId: string) {

return new Promise(resolve => {
  this.total = this.afd.list('users/' + this.cUser.uid + '/budgets/' + budgetId + '/expenses').subscribe((_data) => {


    this.expenseListTotal =  _data.reduce((sum, item) =>sum+item.amount,0);
    resolve(this.expenseListTotal);


  });

  })
}

在组件中:

this.total = this.budgetService.getExpenseListTotals(this.budgetId);
this.total.then(function(value){
 console.log(value)
});