1

I upgraded my angular to ng7. I'm working on a project, where I have 3 http calls, which are depended on each other, so i choose to use switchMap. My problem is that, the way I wrote switchMap before doesn't work. This is how usually wrote my it

DeleteConfirm(id: number, $event) {
console.log('product');
this.productService.deleteProductById(
  id).switchMap(productDeleted => this.productService.getProducts())
  .subscribe(
    products => {
      this.products = products;
      this.confirmDelete = false;
    }, error2 => {}
  );
$event.stopPropagation();

}

The method below is the one i'm working on now. Any suggestion on how I should use the switchMap?

createCompWithGroup(competitionName: string) {

return this.apiService.createACompetition(competitionName)
  .pipe(switchMap(data => {
    const competition = data['category'];
    const competitionSlug = competition.id + '-' + competition.slug;
     this.createSecurityGroup(competitionSlug).subscribe( data =>{
   return   this.addSecurityGroup(competitionName, competitionSlug)
     }
  }
  ));

}

EdwinS95
  • 138
  • 11

1 Answers1

1

This should do it :

createCompWithGroup(competitionName: string) {
  return this.apiService.createACompetition(competitionName).pipe(
    switchMap(data => this
      .addSecurityGroup(competitionName, `${data.cateogry.id}-${data.cateogry.slug}`)
    )
  )
}

The subscribe parameter will be the response of the addSecurityGroup request.