אנגולר - אופרטורים של RXJS לא עובדים עם Firebase
אני משתמש במפעילי RXJS עם האובזרבל של Firebase (distinct ו-filter). זהו עץ ה-Firebase שלי כאן. זה הקוד שלי:
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");
});
אני מנסה להבדיל את הנתונים של Firebase לפי GoogleId. לא קיבלתי שום שגיאה אבל הרשימה המובחנת לא עובדת.
יש איזושהי עזרה או סיבה לכך? תודה.
תשובה #1
נשמע שאתה משתמש במפעיל distinct על מערך במקום על זרם של אובייקטים. כך ש-p הוא מערך במפעיל ה-distinct שלך במקום האובייקט שבתוך המערך כפי שאתה מצפה. ניתן לעשות זאת עם JS רגיל ב-map כך:
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>