我有一個問題,我收到錯誤“core.js:4196 ERROR TypeError: Cannot read properties of undefined (reading 'phones')”
問題是它告訴我我正在經歷的物件陣列是空的,當檢查一點并進行除錯時,我意識到它不是。這實際上不會無限期地出現,資料很大,是的,在 2800 個資料中,它為您帶來了近 5 或 6 部手機(因為那些是注冊的)
無論如何,我真正想做的是從一個物件中獲取“x”用戶的電話號碼并自動完成過濾資料。
我把代碼留給你,也許你會看到我沒有看到的東西:
.TS
contactos: AddContacto[] = [];
filteredContacts: Observable<AddContacto[]>;
phoneControl: FormControl = new FormControl();
this.filteredContactos = this.phoneControl.valueChanges.pipe(
startWith(null),
map((search: string | null) => (search ? this._filterContactos(search) : this.contactos.slice())),
);
private _filterContactos(search: string) {
const filterValue = search.toLowerCase();
return this.contact.filter(
(contact) =>
!!contact.phones.find((email) => contact.includes(search)) ||
contact.fullName.toLowerCase().includes(filterValue),
);
}
.HTML
<div class="containerTitle p-16">
<mat-form-field fxFlex="100" class="mr-12" appearance="outline" fxFlex="100">
<mat-label>Search Client</mat-label>
<input type="text" placeholder="Add email..." matInput [formControl]="phoneControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" >
<mat-option *ngFor="contact of filteredContacts | async" [value]="contact.phones[0]">
{{contact?.phones[0]}} {{contact?.fullName ? '(' contacto?.nombre_completo ')' : ''}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
<small-loading class="loadingProgressBar" [loading]="loading"></small-loading>
</div>
目的
0: {
"state": true,
"coments": null,
"phones": ["22222222"],
"emails": [
"[email protected]"
],
"fullName": "",
"_id": "",
"bitacora": {},
"__v": 0
}
謝謝!!
uj5u.com熱心網友回復:
該錯誤是因為您使用的是舊版本的打字稿,并且有必要檢查陣列是否存在并且長度是否高于 0 才能使用任何陣列方法,如 filter、find、foreach 等。
在您的 _filterContactos 方法中更改它。
private _filterContactos(search: string) {
const filterValue = search.toLowerCase();
return this.contact.filter(
(contact) => {
if (contact.phones && contact.phones.length > 0) {
!!contact.phones.find((email) => contact.includes(search)) ||
contact.fullName.toLowerCase().includes(filterValue)
}
}
);
}
讓我知道它是否對您有用。
uj5u.com熱心網友回復:
您試圖讀取不存在的物件的資料。
err.response.data 試試console.log(err) 和console.log(err.response) 你可能會發現你的回復有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447488.html
標籤:javascript 有角度的 打字稿 筛选 可观察的
