過濾一個數組列表,我使用的觀測在我的角度應用程序,它工作正常。但是,我遇到了一個有點混亂的情況。在一個組件,我成功訂閱可觀察到的,然後過濾結果,就像這樣:使用觀測量和角度應用
this.clientsService.getAll()
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.inactiveRecords = this.records.filter(record => record.category && record.category.includes('inactive'));
this.records = this.inactiveRecords;
},
responseRecordsError => this.errorMsg = responseRecordsError);
然而,在另一組件,而我做幾乎同樣的事情,我正在印刷的錯誤控制檯,上面寫着:
例外:_this.records.filter不是一個函數
這是組件的樣子:
optionReceived(option) {
console.log('Consulting: ' + option.toolbarLabel);
if (option.toolbarLabel === 'OT') {
console.log('Consulting: OT Filter Activated!');
this.clientService.getByCategory('consulting', 1, this.pagesize)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.OTRecords = this.records.filter(record => record.type && record.type.includes('OT'));
this.records = this.OTRecords;
},
responseRecordsError => this.errorMsg = responseRecordsError);
} else {
return this.records;
}
是什麼在第二種情況下的問題?爲什麼我在那裏得到錯誤,而不是在第一種情況下?
如果您在各種情況下懸停在this.records,你看到的數據類型?這兩個例子有什麼不同? – DeborahK
它們看起來是相同的:(屬性)ConsultingComponent.records:任何[] – Muirik
「記錄」被設置爲在兩個成分的空數組。 – Muirik