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

319
Vistas
Angular how to save multiSelect data in firebase

I have a angular form with mulits-select dropdown. the multiselect field markkup is like this:

<mat-form-field appearance="outline">
  <mat-label>Food Categories</mat-label>
   <mat-select 
    ngModel name="categories"
    #categories="ngModel"
    multiple>
   <mat-option *ngFor="let foodCategory of foodCategories | async [value]="foodCategory.categoryName">{{foodCategory.categoryName}}
  </mat-option>
  </mat-select>
  </mat-form-field>

and in my component I am using this code:

onSubmit(form:NgForm){
    console.log(form);
    this.itemService.createItem(
      {categories:[form.value.categories],
      }
    );

In the interface the categories is defined like:

export interface FoodItem {  
    categories: string[];
}

and item-service.ts - the code is like

createItem(foodItem: FoodItem) {
    const foodItemCollection = this.afs.collection<FoodItem>('foodItems');  
  foodItemCollection.add({     
    categories:foodItem.categories,
   }).then(res=>{
    console.log(res);
  });

and when I submit the form the cosole.log for form data shows like this: value: {categories: Array(2)}

There are other fields in the form, but I removed for I think not needed here for the issue I am facing.

The error which is shown is like:

core.js:6479 ERROR FirebaseError: Function addDoc() called with invalid data. Nested arrays are not supported (found in document foodItems/51dDi3OtfMo0NRW90ecd)

Note: when I add the values directly in add-item-component.ts like below it works:

onSubmit(form:NgForm){
        console.log(form);
        this.itemService.createItem(
          {categories:['Chowmin','Chinese'],
          }
        );
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

This was very straight forward if I try to understand the error message.

I was already getting the values as ['Chowmin','Chinese'] and I was nesting it in another array like:

{categories:[form.value.categories],

but It should have been

{categories:form.value.categories}

so further nesting in square-braces was not needed.

about 4 years ago · Santiago Gelvez 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