Angular Firestore如何随机化查询结果并保持数据同步?
问题描述
现有如下用于获取Firestore集合所有记录并同步变更的代码:
this.productsRef.valueChanges({ idField: 'id' });
如何在随机化文档返回顺序的同时,仍保持与Firestore集合及文档的实时同步?
可行解决方案
可以借助lodash的shuffle方法,在订阅到Firestore返回的数据后对结果进行随机排序,这样既保留了实时同步的特性,又实现了顺序随机化:
import { shuffle } from "lodash"; ... this.productsRef.valueChanges({ idField: 'id' }).subscribe((res: Product[])=> { this.products = shuffle(res); });
内容的提问来源于stack exchange,提问作者Jhourlad Estrella
相关产品推荐
相关产品推荐

