categoryIdArray .push is not a function error

Viewed 38

Below code is used to get a categoryid from a checkbox when its checked. I have written a code to convert the formArray data into comma separated string array. Below ill share the code

onChange(categoryId: string[], isChecked: boolean) {
  debugger
  let categoryIdArray
  categoryIdArray = (this.patientReportForm.controls.hubxCategoryId as FormArray);
  if (isChecked) {
    categoryIdArray.push(new FormControl(categoryId)) 
    let desiredFormat= this.patientReportForm.value.hubxCategoryId.join(",")
    this.patientReportForm.setControl('hubxCategoryId', this.formBuilder.group([desiredFormat])); 
  
  } else {
    let index = categoryIdArray.controls.findIndex(x => x.value == categoryId)
    categoryIdArray.removeAt(index);
  }
 
}

here in categoryIdArray.push(new FormControl(categoryId)) gets an error that says "categoryIdArray.push is not a function" when i check more than one checkbox.

my formcontrol

this.patientReportForm = this.formBuilder.group({
      patientId : new FormControl(Number(this.clientId)),
      //hubxCategoryId: new FormControl(),
      hubxCategoryId : this.formBuilder.array([]),
      notes : new FormControl(),
    })

html code

<div *ngFor="let hubxReport of hubxReportList; let i=index">
                <div class="lineheader  "> 
                  <section class="" [formGroup]="patientReportForm">
                    <p><mat-checkbox color="primary" (change)="onChange(hubxReport.categoryId, $event.checked)">{{hubxReport.categoryName}}</mat-checkbox></p>
                  </section>
                </div>
0 Answers
Related