How to POST Iterate Array to webApi Angular

Viewed 40

I'm trying to post my second array and get the id of item() to CreateItemset itemID so how to achieved that? this is what i did but it won't work

 postArray(){
        console.log(this.fg.value)
        this.microSvc.Post(this.fg.value)
        .subscribe((respo)=>{
          this.headerID = respo.id;
          for (const item of this.fg.value?.Info){
            item.id = this.headerID;
            console.log(item.id)
             this.microSvc.Post1(item)
             .subscribe((response)=>{
            for(const itemof this.fg.value?.SetItem){
              item.ItemId = this.detailsID
              this.microSvc.testing(serial).subscribe((res) =>{
                })
             }
              Swal.fire({
              title: 'Added Successfully!',
              icon: 'success',
              heightAuto: false,
              width: 400
             }).then((result) => {
             if (result.isConfirmed){
             }
             }) 
             })
             } 
           })
          }
2 Answers

You should not have a subscribe method called in another subscribe method, please use transformation + higher order mapping operator. I advice you to look into switchMap/mergeMap/exhaustMap/concatMap, and transformation operator like forkJoin/combineLatest.

Try something like this. Use Rxjs ForkJoin. Then you can subscribe to it. Check how ForkJoin works in the official documentation for more info.

  const obs$: Array<Observable<Any>> = []
    
    for(value in values) {
      cont postMethod: Obsevable<any> = service.post(value)
      obs$.push(postMethod)
    }
    
    const result = forkJoin(obs$)
    
    result.subscribe();
Related