【发布时间】:2021-11-24 19:49:27
【问题描述】:
这是我的代码:
getTotalBookListCost(bookList:string[]):number
{
let cost=0;
bookList.forEach(el=>{
this.store.doc("Books/"+el).get().subscribe(data=>{
let temp=<Book>data.data();
cost+=temp.cost;
},error=>this.toastr.error(error.message));
});
return cost;
}
返回的值是 0,我猜是因为返回是在带有 firebase 调用的 forEach 循环之前执行的。如何只在循环执行后才返回?
【问题讨论】:
-
您的 store.doc 是异步函数,您不能像这样更改为 Promise 或 Observable return 一样使用。
标签: angular typescript firebase google-cloud-firestore