我正在使用RXJS运算符和firebase observable(distinct和filter)。 这是我的firebase树 here。 这是我的代码:
let places this.db.list(`users/${this.authProvider.getUID()}/visitedPlaces`,{
query:{
orderByChild:"googleId"
}
});
places.distinct((p)=>{
console.log(p)
return p.googleId;
}).
subscribe((snap)=>{
console.log(JSON.stringify(snap,null,2))
},(err)=>{
console.log(JSON.stringify(err,null,2))
},()=>{
console.log("completed");
});
我正在尝试根据googleId区分firebase数据。 我没有得到任何错误,但不同的列表不起作用。
任何帮助或原因?谢谢。
答案 0 :(得分:0)
听起来你在数组上使用distinct
运算符而不是对象流。因此p
是您的distinct运算符中的数组,而不是您期望的数组内的对象。您可以使用map
中的vanilla js来执行此操作:
Rx.Observable.of([
{ id: 1},
{ id: 2},
{ id: 1},
{ id: 3},
{ id: 2}
]).map(x => x.reduce((a, c) => {
if (!a.some(y => y.id === c.id)) {
a.push(c);
}
return a;
}, [])
).subscribe(x => { console.log(x); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>
或者您可以使用flatMap
扩展数组,如下所示:
Rx.Observable.of([
{ id: 1},
{ id: 2},
{ id: 1},
{ id: 3},
{ id: 2}
])
.flatMap(x => x)
.distinct(x => x.id)
.subscribe(x => { console.log(x); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>