Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

71
Vistas
Populate dropdown from observable

I try to fill an array with values provinding from one service.

The array must fill a dropdown. I follow this post.

The array is defined like this : dropdownList: Array<{id: number, description: string}>;

The service method's is :

async loadFoodTypes(){
    const api: string =  environment.apiAddress + 'FoodType';
    await this.httpClient.get<FoodType[]>(api)
    .pipe()
    .toPromise()
    .then((response: FoodType[]) => {
      this._foodType.next(response);
    })
    .catch(err => console.log(err))
  } 

The FoodType type is defined like this:

 id?: number;
    description: string;
    shortDescription: string;

When I call the service I try to fill array with data.

loadFoodTypes(){
    this.foodtypesService.loadFoodTypes().then(response => 
      this.dropdownList.push({id: <number>(<unknown>response as FoodType).id, description: (<unknown>response as FoodType).description})
    );
  }

My problem is that I have these 2 error messages in the browser console :

Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'id')

and

Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'push')

I presume the second one is linked to the first one.

Thanks for your help.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're not returning anything from loadFoodTypes(). That's why

this.foodtypesService.loadFoodTypes().then(response => 

isn't working. Your response is undefined.

The way that's "more Angular" would be something like

  • In the service:
loadFoodTypes(): void {
    const api: string =  environment.apiAddress + 'FoodType';
    this.httpClient.get<FoodType[]>(api)
    .subscribe(response => this._foodType.next(response))
  } 

...

getFoodTypes(): Observable<FoodType[]> {
   return this._foodType.asObservable();
}

  • In the component:
loadFoodTypes(){
    this.foodtypesService.getFoodTypes()
        .subscribe(foodTypes => 
         this.dropdownList = foodTypes.map((foodType: FoodType) => ({id: foodType.id, description: foodType.description})
        );
  }

And don't forget to unsubscribe from your observables.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is that you don't return the data in the load food type function,

async loadFoodTypes(){
    const api: string =  environment.apiAddress + 'FoodType';
    await this.httpClient.get<FoodType[]>(api)
    .pipe()
    .toPromise()
    .then((response: FoodType[]) => {
      this._foodType.next(response);
      return response;
    })
    .catch(err => console.log(err))
  } 

Also, the code in the catch don't rethrow the error so it will return undefined in the next .then


General notes on your code:

  1. You can remove the .pipe as it
  2. Don't use .toPromise as it deprecated
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda