The error indicates that somewhere in your template, you're trying to use *ngFor on a string value ("Jaw Pain") instead of an array. This is happening in an AutoComplete component.
The issue is likely that some complaint values in your data are still strings when they reach the template. Here are a few solutions:
Solution 1: Ensure all complaint fields are arrays before binding
Add a check to handle cases where complaint might already be an array or might be undefined:
if (this.itemDetail.folloupAppointmentList.length > 0) {
this.itemDetail.folloupAppointmentList.forEach(ele => {
console.log(this.tempComplaintList)
// Check if complaint exists and is a string before processing
if (ele.complaint && typeof ele.complaint === 'string') {
ele.complaint = ele.complaint.split(',').map(c => ({ complaintName: c.trim() }));
} else if (!Array.isArray(ele.complaint)) {
// If complaint is neither string nor array, make it an empty array
ele.complaint = [];
}
ele.startTime = moment(ele.appointmentStartTime, 'hh:mm:ss').format('hh:mm A');
ele.endTime = ele.appointmentEndTime ? moment(ele.appointmentEndTime, 'hh:mm:ss').format('hh:mm A') : null;
});
}
Solution 2: Create a safe getter method
Add a method in your component to safely return complaints as an array:
getComplaintsAsArray(complaints: any): any[] {
if (!complaints) return [];
if (Array.isArray(complaints)) return complaints;
if (typeof complaints === 'string') {
return complaints.split(',').map(c => ({ complaintName: c.trim() }));
}
return [];
}
Then in your template, use:
<p-autoComplete [(ngModel)]="item.complaint"
[suggestions]="tempComplaintList"
[ngModelOptions]="{standalone: true}"
[multiple]="true">
<ng-template let-complaint pTemplate="item">
<div *ngFor="let comp of getComplaintsAsArray(complaint)">
{{ comp.complaintName }}
</div>
</ng-template>
</p-autoComplete>
Solution 3: Initialize complaints as arrays from the start
Make sure when you're creating or fetching the data initially, complaints are always arrays:
// When initializing a new appointment
this.newAppointment = {
complaint: [], // Initialize as empty array
// ... other properties
};
The key issue is that the AutoComplete component expects an array for the multiple="true" mode, but it's receiving a string. Make sure all complaint fields are properly converted to arrays before they're used in the template.