Home > other >  I want to send post request in angular 13. The first request response returns id which i want to pos
I want to send post request in angular 13. The first request response returns id which i want to pos

Time:09-23

I want to send post request in angular 13. The first request response returns id which i want to post in my second request and then that response i want to send in third post request. now i am very much confused how to works with pipes and switch map. currently i am nesting subscribe but the issue is id from first post is assigned but second one returns undefined though when i check in log value is there in object form. what could be the problem. attached is my code:

this.headOffice.createOffice(data) .subscribe( (res) => { this._officeId = res.id;//this is working this.submited=true;console.log(this.submited);

      console.log('Response Office ' JSON.stringify(res));

      const officeAddress = this.setOfficeAddress();
      this.headOffice.createOfficeAddress(officeAddress)
      .subscribe(
        (resp) => {
         
          console.log('response from office Address '  resp);//this shows object with id
          this._officeAddressId=resp.id; //this gives undefined here is the error
          console.log(this._officeAddressId);            
          const officeContact = {
            active: true,
            userId: 0,
            dbName: null,
            apiStatus: "string",
            id: 0,
            officeAddressId: this._officeAddressId,
            contactTypeid: 1,
            countryAreaCode: "string",
            contactNumber: this.contact,
            extention: this.headOfficeInfo?.ext,
            contactTypeName: "string"
          }
          this.headOffice.createOfficeContact(officeContact)
          .subscribe(
            (respp) => {
              console.log('response from contact '   respp);
              //move to new form
              this.submited = true;
            }
          )
        }
      );
    });

  return true;[![Code image file][1]][1]

Please help me as i am out of resource guide me

CodePudding user response:

Chaining HTTP calls is done with switchMap, like this :


this.headOffice.createOfficeAddress(officeAddress).pipe(
  map(res => ({
    active: true,
    userId: 0,
    dbName: null,
    apiStatus: "string",
    id: 0,
    officeAddressId: res.id,
    contactTypeid: 1,
    countryAreaCode: "string",
    contactNumber: this.contact,
    extention: this.headOfficeInfo?.ext,
    contactTypeName: "string"
  })),
  switchMap(param => this.headOffice.createOfficeContact(param)),
  tap(() => this.submitted = true)
)

  • Related