How can i filter an Observable/BehaviourSubject correctly?

Service:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Injectable({
providedIn: 'root'
})
export class DeckService{
private decks$ = new BehaviorSubject<Deck[]>([]);
constructor(private http: HttpClient) {}
public init(): void {
this.http.get<Deck[]>('/api/decks').subscribe((decks : Deck[]) => {
this.decks$.next(decks);
});
}
public getDecks(): Observable<Deck[]> {
return this.decks$.asObservable();
}
public getDeck(deck_id : number) : Observable<Deck> {
return this.decks$.pipe(
map(decks => decks.filter(deck => deck.deck_id === deck_id)[0])
);
}
}
</code>
<code>@Injectable({ providedIn: 'root' }) export class DeckService{ private decks$ = new BehaviorSubject<Deck[]>([]); constructor(private http: HttpClient) {} public init(): void { this.http.get<Deck[]>('/api/decks').subscribe((decks : Deck[]) => { this.decks$.next(decks); }); } public getDecks(): Observable<Deck[]> { return this.decks$.asObservable(); } public getDeck(deck_id : number) : Observable<Deck> { return this.decks$.pipe( map(decks => decks.filter(deck => deck.deck_id === deck_id)[0]) ); } } </code>
@Injectable({
  providedIn: 'root'
})
export class DeckService{
  private decks$ = new BehaviorSubject<Deck[]>([]);

  constructor(private http: HttpClient) {}

  public init(): void {
    this.http.get<Deck[]>('/api/decks').subscribe((decks : Deck[]) => {
      this.decks$.next(decks);
    });
  }

  public getDecks(): Observable<Deck[]> {
    return this.decks$.asObservable();
  }

  public getDeck(deck_id : number) : Observable<Deck> {
    return this.decks$.pipe(
      map(decks => decks.filter(deck => deck.deck_id === deck_id)[0])
    );
  }

}

Component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
public decks$ : Observable<Deck[]>;
public deckSelected$! : Observable<Deck | undefined>;
constructor(private deckService : DeckService) {
this.decks$ = this.deckService.getDecks();
this.deckService.init();
}
ngOnInit() {
this.deckSelected$ = this.deckService.getDecks().pipe(
map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58))
);
}
}
</code>
<code> public decks$ : Observable<Deck[]>; public deckSelected$! : Observable<Deck | undefined>; constructor(private deckService : DeckService) { this.decks$ = this.deckService.getDecks(); this.deckService.init(); } ngOnInit() { this.deckSelected$ = this.deckService.getDecks().pipe( map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58)) ); } } </code>

  public decks$ : Observable<Deck[]>;
  public deckSelected$! : Observable<Deck | undefined>;

  constructor(private deckService : DeckService) {
    this.decks$ = this.deckService.getDecks();
    this.deckService.init();
  }
  
  ngOnInit() {
    this.deckSelected$ = this.deckService.getDecks().pipe(
      map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58))
    );
  }


}

HTML:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div *ngIf="deckSelected$ | async as deck">
<h2>{{deck.deck_id}}</h2>
</div>
</code>
<code><div *ngIf="deckSelected$ | async as deck"> <h2>{{deck.deck_id}}</h2> </div> </code>
<div *ngIf="deckSelected$ | async as deck">
  <h2>{{deck.deck_id}}</h2>
</div>

Deck Model:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export interface Deck {
deck_id: number;
name: string;
description: string;
}
</code>
<code>export interface Deck { deck_id: number; name: string; description: string; } </code>
export interface Deck {
  deck_id: number;
  name: string;
  description: string;
}

So i want to get a card deck specified by a id. Therefore i want to first fetch all decks in an observable and then filter it.
Because using two endpoints ist overkill as i already have them loaded in the app.

However when running this it just gives me an blank page without any rendered items.

0

Generally the service contains only the http client API calls, the component will contain the subscribe part of the code, this is because you can control the order of execution of observable calls.

In the below service, there is no subscribe inside the methods, instead replaced with tap to perform the side effects like setting the value for the BehaviorSubject.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Injectable({
providedIn: 'root'
})
export class DeckService{
private decks$ = new BehaviorSubject<Deck[]>([]);
constructor(private http: HttpClient) {}
public init() {
return this.http.get<Deck[]>('/api/decks').pipe(
tap((decks : Deck[]) => {
this.decks$.next(decks);
})
);
}
public getDecks(): Observable<Deck[]> {
return this.decks$.asObservable();
}
public getDeck(deck_id : number) : Observable<Deck> {
return this.decks$.pipe(
map(decks => decks.filter(deck => deck.deck_id === deck_id)[0])
);
}
}
</code>
<code>@Injectable({ providedIn: 'root' }) export class DeckService{ private decks$ = new BehaviorSubject<Deck[]>([]); constructor(private http: HttpClient) {} public init() { return this.http.get<Deck[]>('/api/decks').pipe( tap((decks : Deck[]) => { this.decks$.next(decks); }) ); } public getDecks(): Observable<Deck[]> { return this.decks$.asObservable(); } public getDeck(deck_id : number) : Observable<Deck> { return this.decks$.pipe( map(decks => decks.filter(deck => deck.deck_id === deck_id)[0]) ); } } </code>
@Injectable({
  providedIn: 'root'
})
export class DeckService{
  private decks$ = new BehaviorSubject<Deck[]>([]);

  constructor(private http: HttpClient) {}

  public init() {
    return this.http.get<Deck[]>('/api/decks').pipe(
      tap((decks : Deck[]) => {
        this.decks$.next(decks);
      })
    );
  }

  public getDecks(): Observable<Deck[]> {
    return this.decks$.asObservable();
  }

  public getDeck(deck_id : number) : Observable<Deck> {
    return this.decks$.pipe(
      map(decks => decks.filter(deck => deck.deck_id === deck_id)[0])
    );
  }
}

In the controller, you can make the main api call that fetches the decks then just filter the returned value directly instead of relying on the behaviour subject.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public decks$ : Observable<Deck[]>;
public deckSelected$! : Observable<Deck | undefined>;
constructor(private deckService : DeckService) {
this.decks$ = this.deckService.getDecks();
this.deckSelected$ = this.deckService.init().pipe(
tap((decks: Deck[]) => decks.find(deck => deck.deck_id === 58)),
);
}
ngOnInit() {
this.deckSelected$ = this.deckService.getDecks().pipe(
map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58))
);
}
}
</code>
<code> public decks$ : Observable<Deck[]>; public deckSelected$! : Observable<Deck | undefined>; constructor(private deckService : DeckService) { this.decks$ = this.deckService.getDecks(); this.deckSelected$ = this.deckService.init().pipe( tap((decks: Deck[]) => decks.find(deck => deck.deck_id === 58)), ); } ngOnInit() { this.deckSelected$ = this.deckService.getDecks().pipe( map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58)) ); } } </code>
  public decks$ : Observable<Deck[]>;
  public deckSelected$! : Observable<Deck | undefined>;

  constructor(private deckService : DeckService) {
    this.decks$ = this.deckService.getDecks();
    this.deckSelected$ = this.deckService.init().pipe(
      tap((decks: Deck[]) => decks.find(deck => deck.deck_id === 58)),
    );
  }
  
  ngOnInit() {
    this.deckSelected$ = this.deckService.getDecks().pipe(
      map((decks: Deck[]) => decks.find(deck => deck.deck_id === 58))
    );
  }
}

2

Your code actually works, I’m assuming your test data doesn’t have an items where deck_id===58.

Your service already has a method that does the filtering based on the deck_id, so there’s no need to filter both in the component and in the service, so let’s put the filtering logic in only one place. A case could be made for either, but I’ll go with leaving it in the service.


Also, as Naren Murali, mentioned, generally it’s beneficial to only subscribe in your components, not in your services. This allows that your observables to remain lazy and only fetch data when there is a subscriber.

In your service, we can simplify by declaring decks$ as an observable, rather than use a Subject. This alleviates the need to subscribe.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class DeckService{
public decks$ = this.http.get<Deck[]>('/api/decks').pipe(shareReplay(1));
constructor(private http: HttpClient) { }
public getDeck(deck_id: number): Observable<Deck|undefined> {
return this.decks$.pipe(
map(decks => decks.find(deck => deck.deck_id === deck_id))
);
}
}
</code>
<code>export class DeckService{ public decks$ = this.http.get<Deck[]>('/api/decks').pipe(shareReplay(1)); constructor(private http: HttpClient) { } public getDeck(deck_id: number): Observable<Deck|undefined> { return this.decks$.pipe( map(decks => decks.find(deck => deck.deck_id === deck_id)) ); } } </code>
export class DeckService{
  public decks$ = this.http.get<Deck[]>('/api/decks').pipe(shareReplay(1));

  constructor(private http: HttpClient) { }

  public getDeck(deck_id: number): Observable<Deck|undefined> {
    return this.decks$.pipe(
      map(decks => decks.find(deck => deck.deck_id === deck_id))
    );
  }
}

Here we used shareReplay to ensure only a single http call is made and also to reuse the previous result on future subscriptions.

The getDecks method is no longer needed since you already have decks$ observable.

You can also simplify the component. It’s not necessary to split the declaration and initialization. We can do it all at once like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class Component {
public decks$ = this.deckService.decks$;
public deckSelected$ = this.deckService.getDeck(58);
constructor(private deckService : DeckService) { }
}
</code>
<code>export class Component { public decks$ = this.deckService.decks$; public deckSelected$ = this.deckService.getDeck(58); constructor(private deckService : DeckService) { } } </code>
export class Component {

  public decks$ = this.deckService.decks$;
  public deckSelected$ = this.deckService.getDeck(58);

  constructor(private deckService : DeckService) { }

}

Here’s a Stackblitz of this simplified code.


Of course you’ll want to remove the hardcoded id of 58 and make it changeable. This is a good place to use a Subject, which you could do like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> private deckId$ = new Subject<number>();
public deckSelected$ = this.deckId$.pipe(
switchMap(id => this.deckService.getDeck(id))
);
public selectDeck(deck: Deck) {
this.deckId$.next(deck.deck_id);
}
</code>
<code> private deckId$ = new Subject<number>(); public deckSelected$ = this.deckId$.pipe( switchMap(id => this.deckService.getDeck(id)) ); public selectDeck(deck: Deck) { this.deckId$.next(deck.deck_id); } </code>
  private deckId$ = new Subject<number>();

  public deckSelected$ = this.deckId$.pipe(
    switchMap(id => this.deckService.getDeck(id))
  );

  public selectDeck(deck: Deck) {
    this.deckId$.next(deck.deck_id);
  }

Here’s a working Stackblitz

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật