Angular Firestore:随机化查询结果

问题描述 投票:0回答:1

如果我有下面的代码来引用所有记录并同步更改:

this.productsRef.valueChanges({ idField: 'id' });

如何随机化我获取的文档的顺序,同时保持集合和文档与 Firestore 的同步?

更新:我玩了一下,这似乎有效:

import { shuffle } from "lodash";
...    
this.productsRef.valueChanges({ idField: 'id' }).subscribe((res:Product[])=> {
   this.products = shuffle(res);
});
angular google-cloud-firestore angularfire
1个回答
0
投票

将我的评论转化为答案

您找到的解决方法似乎不错,但您可以在第三行添加

uniq()
,即

this.products = uniq(shuffle(res)); 

这样文档只会出现一次。

© www.soinside.com 2019 - 2024. All rights reserved.