Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!