In Angular-12 project I have this JSON API Response:
{
"message": "Employee Detail.",
"error": false,
"code": 200,
"results": {
"employee": {
"id": 8,
"first_name": "JONAH",
"last_name": "YAKUBU",
"other_name": "AKWETEY",
"employeecontacts": [
{
"id": 1,
"phone_type_id": 2,
"employee_id": 8,
"phone_number": "014566778",
"is_primary_contact_number": 1,
"phonetypes": {
"id": 2,
"type_name": "Arena",
}
},
{
"id": 2,
"phone_type_id": 3,
"employee_id": 8,
"phone_number": "016766709",
"is_primary_contact_number": 0,
"phonetypes": {
"id": 2,
"type_name": "Office",
}
}
]
}
}
}
Interface:
export class EmployeeResponse {
results!: { employee: IEmployee; };
}
export interface IEmployee {
id?: number;
first_name?: string;
other_name?: string;
last_name?: string;
employeecontacts?: IContact[];
}
export interface IContact {
id?: number;
phone_number: string;
phone_type_id?: number;
phonetypes?: {id:number,type_name:string};
is_primary_contact_number?: boolean;
}
service:
getEmployeeById(id: number): Observable<EmployeeResponse> {
return this.http.get<EmployeeResponse>(this.api.baseURL + 'employees/fetchbyid/' + id, this.httpOptions);
}
public updateEmployee(id: number, employee: IEmployee): Observable<any> {
return this.http.put(this.api.baseURL + 'employees/update/' + id, employee, this.httpOptions);
}
component:
_id!: number;
contactInfoForm!: FormGroup;
contactdata!: IEmployee;
ngOnInit(): void {
this.isLoading = true;
this._id = this.route.snapshot.params['id'];
this.updateContact();
this.loadContactById();
}
loadContactById() {
this.employeeService
.getEmployeeById(this._id)
.subscribe((data: EmployeeResponse) => {
this.contactdata = data.results.employee;
this.contactInfoForm.patchValue({
first_name: this.contactdata.first_name,
});
});
this.contactInfoForm.setControl('contacts', this.SetExistingContacts(this.contactdata.employeecontacts));
}
SetExistingContacts(contactSets: IContact[]): FormArray {
const formarray = new FormArray([]);
contactSets.forEach(c => {
formarray.push(this.fb.group({
phone_number: c.phone_number,
phone_type_id: c.phone_type_id,
is_primary_contact_number: c.is_primary_contact_number
}));
});
return formarray;
}
updateContact() {
this.contactInfoForm = this.fb.group({
id: [''],
first_name: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(50)]],
contacts: this.fb.array([
this.addContactFormGroup()
])
});
}
addContactFormGroup(): FormGroup {
return this.fb.group({
phone_type_id: ['', Validators.required],
phone_number: ['', [Validators.required, Validators.maxLength(15)]],
is_primary_contact_number: ['']
});
}
public addContactButtonClick() {
const contacts = this.contactInfoForm.get('contacts') as FormArray
contacts.push(this.addContactFormGroup())
}
get contacts() {
return this.contactInfoForm.controls['contacts'] as FormArray;
}
getContactFormGroup(index: number): FormGroup {
return this.contacts.at(index) as FormGroup;
}
I am tying to load data into the Dynamic FormArray and then update it.
I got this error from the component:
Argument of type 'IContact[] | undefined' is not assignable to parameter of type 'IContact[]'.
Type 'undefined' is not assignable to type 'IContact[]'.ts(2345)
And this line is highlighted:
(this.contactdata.employeecontacts));
in:
this.contactInfoForm.setControl('contacts', this.SetExistingContacts(this.contactdata.employeecontacts));
How do I resolve the error and then update the data?
Thanks