I have written some TypeScript code for a function that checks the client_id in my application.
public clients: Client[];
ngOnInit() {
this.cs.getAllClients().subscribe(
clients => {
this.clients = clients;
console.log(clients)
}
);}
getName(clientid: string) {
const [filteredClient] = this.clients.filter(pt => pt.client_id === clientid);
if (typeof filteredClient !== 'undefined' && clientid === filteredClient.client_id) {
console.log(filteredClient.clientName)
return filteredClient.clientName;
}
}
This function is used in the HTML code as well,
<table class="bordered table-bordered" [mfData]=" sale | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage"
[(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
<thead style="color:black; background:rgb(207, 235, 245);border:1px solid rgb(190, 190, 190);">
<tr>
<th>
<mfDefaultSorter by="client_name">Name</mfDefaultSorter>
</th>
<th>
<mfDefaultSorter by="sale_date">Data</mfDefaultSorter>
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of mf.data">
<td>{{getName(item.clientName)}}</td>
<td>{{item.sale_date | date:'d/MM/yyyy'}}</td>
</tr>
</tbody>
</table>
However, when I try to display the client name, it doesn't show up and I get an error in the console saying 'Cannot read property 'filter' of undefined.'
Could someone please help me identify the issue with this code?
Thank you in advance!