I'm trying to learn rxjs and the Observable concept in general and have a scenario where I have a class of <Room>{} where <Player>{} can join in many-to-many relationship style.
In Firestore I have collection of rooms where each room has a property called players which is an array of user uids.
When a rooms component is created I subscribe to _roomService.getPlayersInRoom(roomId) which looks like below:
getPlayersInRoom(roomId: string) {
return this._db.doc<Room>(`rooms/${roomId}`).valueChanges().pipe(
map(room => room.players),
switchMap(players => players),//2
switchMap(playerId => {
if(playerId) {
return this._db.doc<Player>(`users/${playerId}`).valueChanges();
}
})
);
}
I subscribe to it later with
.subscribe(player => {
if (player) {
this.players = new Array();
this.players.push(player);
}
There are couple of issues here. My observable does not return an array of players as expected (see line //2 which transforms the string[] into a string)
Another issues is that I new up the this.players array in my component every time that room changes (otherwise the .push() will push in duplicates.
I've read documentation on some of these operators and I understand them somewhat but not enough to figure out why this code is not behaving the way it should.