【发布时间】:2018-06-24 01:46:04
【问题描述】:
在使用 FireStore 和 Angular 5 时,我的数据作为 Observables 交付。这意味着每次数据库中的数据发生变化时,都会导致一次新的读取(因此据我了解,我要为此付费)。
我不想要这种“不断重新加载,不断充电”的功能。 我想要一次读取,将我的数据存储在数组中,然后关闭连接。
这是我当前的代码。如何修改我的服务以在从 FireStore 检索产品时将产品存储在一个数组中,以便对我的服务的所有后续调用都从该数组(如缓存)返回数据,而无需再次调用 FireStore?
product.service.ts
products: Observable<Product[]>; // *** I want this to be an array of Product objects, not an observable
getProductsAll(): Observable<Product[]> {
return this.afs.collection( 'products', ref => ref.orderBy( 'published', 'desc' ) ).snapshotChanges()
.map( actions => {
return actions.map( a => {
const id = a.payload.doc.id;
const data = a.payload.doc.data() as Product;
return { id, ...data };
})
})
products.component.ts
items: any;
ngOnInit(): void {
this.productService.getProductsAll()
.subscribe( products => {
this.items = products;
})
}
就上下文而言,我的小测试应用程序导致读取过多并达到 FireStore 每天 50,000 次读取的配额,而数据库中只有 100 个文档。
【问题讨论】:
标签: angular google-cloud-firestore