[Fixed] Check if exist data in table angular

Issue

I have this function :

  onSubmit() {
     this.http.getDataFromServer('api/all').subscribe(resp => {
       console.log(resp.data)
    })

    this.http.postDataToServer('api/in', this.companyForm.value).subscribe(() => {
      //show that organization is added in dialog
      this.dialog.open(DisplayPopupComponent, {
        data: {
          title: "Organization Updated Successfully!"
        }
      });
    })
  }

We have data in resp.data, i want use this data out of this query to check if equal NULL or not to implement second query.

Graph :

resp.data —–> exist data? then apply second query (api/in).

      -----> NULL data do nothing.

Solution

I think you need to use switchMap.

Here is an example:

  onSubmit() {
    this.http.getDataFromServer('api/all').pipe(
      switchMap(resp => {
        if (resp.data) {
          return this.http.postDataToServer('api/in', this.companyForm.value);
        }
      })
    ).subscribe(() => {
      //show that organization is added in dialog
      this.dialog.open(DisplayPopupComponent, {
        data: {
          title: "Organization Updated Successfully!"
        }
      });
    });
  }

Leave a Reply

(*) Required, Your email will not be published