0
I’m trying to create some dynamic checkboxes, but when I run the browser informs
"ERROR Typeerror: Cannot read Property 'map' of Undefined"
Because?
export class FormPostsComponent implements OnInit {
form: FormGroup;
categories: Category[];
constructor(
private categoriesService: CategoriesService,
private formBuilder: FormBuilder
) { }
ngOnInit() {
this.categoriesService.read().subscribe(categories => this.categories = categories);
this.form = this.formBuilder.group({
id: [null],
title: [null, [Validators.required, Validators.minLength(3)]],
body: [null, [Validators.required]],
categories: this.buildCategories(),
tags: [null],
imgFeatured: [null],
slug: [null]
});
}
buildCategories() {
const values = this.categories.map(x => new FormControl(false));
return this.formBuilder.group(values);
}
}