1

I am doing a post on server and trying to console output the response http code. I used chrome tool as debugger to check response and I can see the output coming but at the end it goes to subscribe library does something and sets the function rather than value.

My component class is

 export class MicroserviceCreateComponent implements OnInit{

   errorMessage: string;
   postMessage: string;
   teams: Team[];
   microservice: Microservice[];

    constructor(private microserviceService: MicroserviceService) { }

    ngOnInit() {
    }

  onSubmit({ value, valid }: { value: Microservice, valid: boolean }) {
     this.createMicroservice(value); 
     console.log(this.postMessage); <<<<<<<<==== This gives me undefined
  }

   createMicroservice(value){
      this.microserviceService.createMicroservice(value).subscribe(
        postMessage => this.postMessage = postMessage.valueOf(),
         error => {
          this.errorMessage = error.getMessage();
          console.error(error.getDescription());
        });
  }
}

enter image description here

amulamul
  • 372
  • 3
  • 14

1 Answers1

1

this.microserviceService.createMicroservice(value).subscribe( . . . ) is a non-blocking asynchronous call. Meaning that after you call it console.log(this.postMessage) is free to print before the web request is complete. Try putting the console.log(this.postMessage) in the subscription.

this.createMicroservice(value); // This is an async call
console.log(this.postMessage); // This happens immediately afterwards
Pearman
  • 1,068
  • 5
  • 14