The error indicates that your *ngFor is trying to iterate over a string ("Jaw Pain") instead of an array. This suggests that either the transformation isn't working for all items, or some complaints are already strings instead of comma-separated values.
Here's the updated code with better error handling:
if (this.itemDetail.folloupAppointmentList.length > 0) {
this.itemDetail.folloupAppointmentList.forEach(ele => {
console.log(this.tempComplaintList);
// Check if complaint exists and handle different data types
if (ele.complaint) {
if (typeof ele.complaint === 'string') {
// If it's a string, split by comma and create objects
ele.complaint = ele.complaint.split(',').map(c => ({complaintName: c.trim()}));
} else if (Array.isArray(ele.complaint)) {
// If it's already an array, check if items are strings or objects
ele.complaint = ele.complaint.map(c => {
if (typeof c === 'string') {
return {complaintName: c.trim()};
}
return c; // Already an object
});
}
} else {
// If complaint doesn't exist, initialize as 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;
});
}
Also check your template - make sure you're iterating over the correct property:
<!-- Correct way -->
<div *ngFor="let complaint of appointment.complaint">
{{ complaint.complaintName }}
</div>
<!-- NOT this (this would cause the error) -->
<div *ngFor="let complaint of appointment.complaint.complaintName">
Additional debugging - Add this console log to see what the data looks like:
console.log('Before transformation:', ele.complaint);
// ... transformation code ...
console.log('After transformation:', ele.complaint);
This should handle cases where:
complaint is a comma-separated string
complaint is already an array of strings
complaint is already an array of objects
complaint is undefined/null