I have implemented a function to retrieve all clients from an API:
this.ws.getallclients().subscribe(
client => {
this.client = client.map((clients) => {
this.filteredOptions = this.addsale.controls['client_id'].valueChanges.pipe(
startWith(''),
map(val => this._filter(val))
);
return new Client(clients);
});
if (this.ss.getData('client_id')) {
const client_id = this.ss.getData('client_id');
this.addsale.controls.client_id.setValue(client_id)
let selectedClient = new Client('')
this.selectedClient = null;
for (let i = 0; i < client.length; i++) {
if (client[i].client_id === client_id) {
this.selectedClient = client[i];
}
}
}
}
);
An error is being displayed as follows:
ERROR TypeError: Cannot read property 'phone' of null at Object.eval [as updateRenderer]
This error occurs because the value `client[i].client_id` returns a numeric ID like 123456, while it should display the name of the client such as "MyName". This mismatch causes the issue in the following HTML code:
Here is the relevant html code snippet:
<fieldset>
<legend>Client Data</legend>
<div class="input-field col s4">
<input formControlName="client_id" id="client_id" matInput placeholder="Select Client*" aria-label="State" [matAutocomplete]="auto" >
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option (onSelectionChange)="onSelect(item.client_id)" *ngFor="let item of filteredOptions | async" [value]="item.name">
{{item.name}}
</mat-option>
</mat-autocomplete>
</div>
<div class="input-field col s12">
ID Number:
<span style="color:darkblue">{{selectedClient.phone}}</span>
</div>
</fieldset>
Any suggestions on how to resolve this error?