[Fixed] Service values still undefined after subscribing


I have a function that is called once a user selects a value in a form:

onChange($x: any){
      .subscribe(data => this.carListAfterManufacturerSelect = data);

The service:

getCarsByManufacturer(manufacturer: string): Observable<Cars[]>{
    return this.http.get<Cars[]>(this.apiBaseUrl + '/cars/spec/' + manufacturer)

I want to use the user’s selected value as a parameter for my function but carListAfterManufacturerSelect keeps returning undefined. Any suggestions?


Here is how subscribe works:

onChange($x: any){
  this.ApiService.getCarsByManufacturer($x.target.value).subscribe(data => {
    this.carListAfterManufacturerSelect = data;
    console.log('2. Received !', this.carListAfterManufacturerSelect); // filled
  console.log('1. Not received yet', this.carListAfterManufacturerSelect); // undefined

codeToBeExecutedAfterHttpCallIsReceived() {
  console.log('3. Received too !', this.carListAfterManufacturerSelect); // filled
  // do a lot of complex stuff with carListAfterManufacturerSelect

Leave a Reply

(*) Required, Your email will not be published