1

I want to replace subscribe in subscribe by switchMap, i dont know how to do that. I need 1 example, but i didn't find it on the Internet.

dialogRef.afterClosed().pipe(filter(result => !!result)).subscribe(result => {
            result.owner = this.user.id;
            proj._newname = result.name;
            proj.name = result.name;
            this.projectService.updateProject(proj, result.name)
                .subscribe(project => {
                        const pw = new ProjectWorker({
                            approve: true,
                            worker: this.user.id,
                            project: project.id,
                            admin: true,
                            write: true
                        });
                        this.projectService.createProjectWorker(pw).subscribe(_ => {
                            this.getProjects();
                        });
                    },
                    err => {
                        this._snackbar.open('Project already exist', 'error', {
                            panelClass: 'snackbar-error'
                        })
                    });
            }
        );
  • Does this answer your question? [Angular Subscribe within Subscribe](https://stackoverflow.com/questions/55447803/angular-subscribe-within-subscribe) – wentjun Apr 24 '20 at 02:19

1 Answers1

1

Something like this:

dialogRef.afterClosed().pipe(
  filter(result => !!result),
  switchMap((result) => {
    result.owner = this.user.id;
    proj._newname = result.name;
    proj.name = result.name;
    return this.projectService.updateProject(proj, result.name);
  }),
  switchMap((project) => {
    const pw = new ProjectWorker({
      approve: true,
      worker: this.user.id,
      project: project.id,
      admin: true,
      write: true
    });
    return this.projectService.createProjectWorker(pw);
  }),
).subscribe(
  res => this.getProjects(),
  err =>
    this._snackbar.open('Project already exist', 'error', {
      panelClass: 'snackbar-error'
    })
);
eko
  • 39,722
  • 10
  • 72
  • 98