RXJS运营商不使用firebase

时间:2018-01-29 13:53:03

标签: angular typescript firebase firebase-realtime-database rxjs

我正在使用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数据。 我没有得到任何错误,但不同的列表不起作用。

任何帮助或原因?谢谢。

1 个答案:

答案 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>