kate moss
kate moss

Reputation: 766

Call two Nodejs API in one click in Angular?

I have two APIs deleteArticlesData and deleteArticlesDataMongo which I need to call together in one click , but currently it is calling one and then alert to click one more time for second api call.

article.service.ts

    deleteArticlesData(payload) {
    return this.http.post(environment.api_url + '/articles/articlesDelete', payload)
    }
    deleteArticlesDataMongo(payload) {
    return this.http.post(environment.api_url + '/articles/articlesById', payload)
    } 

main.component.html

   <button type="button" class="modalbtn1" (click)="deleteArticle(articleDetails.articleid)" 
   (click)="deleteArticleMongo(articleDetails.articleid)">Delete</button>

main.component.ts

    deleteArticle(id) {


    if (confirm('Are you sure you want to delete?')) {
      this.article.deleteArticlesData({ articleid: id }).subscribe(data => {
        console.log(data);
        console.log(this.userId, this.username);
        console.log(Date);
      })
    }
  }

  deleteArticleMongo(id) {


    if (confirm('Are you sure you want to delete?')) {
      this.article.deleteArticlesDataMongo({ articleid: id }).subscribe(data => {
        console.log(data);
        console.log(this.userId, this.username);
        console.log(Date);
      })
    }
  } 

Upvotes: 1

Views: 25

Answers (1)

StPaulis
StPaulis

Reputation: 2916

You can use forkJoin to make parallel calls to your backend.

main.component.html

   <button type="button" 
           class="modalbtn1" 
           (click)="deleteArticleAndArticleMongo(articleDetails.articleid)">
       Delete
   </button>

main.component.ts

deleteArticleAndArticleMongo(id: string) {
  if (confirm('Are you sure you want to delete?')) {
      forkJoin([
          this.article.deleteArticlesDataMongo({ articleid: id }),
          this.article.deleteArticlesData({ articleid: id })
        ]
      ).subscribe(data => {
        console.log(data); // data should be an array with both results
        console.log(this.userId, this.username);
        console.log(Date);
    })
  }
}

Upvotes: 3

Related Questions