I have 2 observable coming from firebase with angularfire and using rxjs
private boatsCollection: AngularFirestoreCollection<Boat>;
boats: Observable<Boat[]>;
private bookingsCollection: AngularFirestoreCollection<Booking>;
bookings: Observable<Booking[]>;
structures are not marching but bookings
collection documents have ID'S
containing information about boat
(so boat id
)
I want to to filter boats and leave only these documents where ids are not matching to bookings collection document(s) ID
value.
How this can be achieved with rxjs?
ngOnInit() {
// Subscribe to route params and get user's selected date
this.sub = this.route.params.subscribe(params => {
this.date = Number (params['date']);
// get bookings where selected date exits
this.bookingsCollection = this.afs.collection<Booking>('bookings', ref => ref.where('date', '==', this.date));
this.bookings = this.bookingsCollection.valueChanges({ idField: 'id' });
// Boats Collection
this.boatsCollection = this.afs.collection<Boat>('boats');
this.boats = this.boatsCollection.valueChanges({ idField: 'id' });
combineLatest(this.boats, this.bookings).subscribe(
([boats, bookings]) => {
const bookingIds = bookings.map(booking => booking.boatID);
const filteredBoats = boats.filter(boat => !(bookingIds.includes(boat.id)));
console.log(filteredBoats);
},
);
});
}