Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

320
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda