0

I am sending Data by POST method from angular, POST api returns ID from spring boot but i am unable to assign the returned id to variable in angular.

onSubmit(){

    if(this.type=1){
      this.milage = 30;
      this.fixedCostPerKm = this.costOfFuel/this.milage;
    }
    else if(this.type=2){
      this.milage = 25;
      this.fixedCostPerKm = this.costOfFuel/this.milage;
    }
    else if(this.type=3){
      this.milage = 20;
      this.fixedCostPerKm = this.costOfFuel/this.milage;
    }
    
    this.costKmWise = this.fixedCostPerKm * this.noOfKm;
    this.totalCost = this.costKmWise + this.wearAndTearCost + this.driverCharges + this.appCharges;
    this.costPerPerson = this.totalCost / this.noOfOccupants;

    this.bills = {
      rideId: this.rideId,
      totalbill: this.totalCost,
      date: this.date,
      costPerOccupant: this.costPerPerson
    }

    
    this.myService.addNewBill(this.bills).subscribe((data: any) => {
      console.log(data);
      this.billId = data;
    });

    console.log("ID "+this.billId);

  }

In console billId is showing undefined but data is printing ID.

In MyService class this is the that is calling API.

addNewBill(data:any){
    return this.httpClient.post("http://localhost:8080/api/bill/new",data);
  }

POST api returns ID.

I am Expecting to assign id to this.billId.

  • The subscribe handler is asynchronously called, after the request completes. The `console.log("ID "+this.billId);` happens before the request completes. – Octavian Mărculescu Jul 11 '23 at 13:34

0 Answers0