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)
     }
  }
  ));

}

3
  • I'm a bit confused, the two code samples doesn't seem to be related in any way Commented Oct 25, 2018 at 10:52
  • The first sample is from a project where i used angular 4-5 Commented Oct 25, 2018 at 11:46
  • Yes, but old sample isn't supposed to do the same thing as the new one, right? I don't see the point in showing the previous code if they don't try to achieve the same thing Commented Oct 25, 2018 at 11:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.