1 回答

TA贡献1798条经验 获得超3个赞
由于您正在进行异步调用,因此在控制到达filteredData语句时,cachedResults不会从服务中获取值。这就是undefined错误的原因。
要解决该问题,您必须在服务调用完成并返回数据作为响应后执行该语句。
ngOnInit() {
this.psService.tDate
.subscribe(x => {
this.cachedResults = x;
this.filterData = [...new Set(this.cachedResults.map(item => item.cus_name))].filter(Boolean);
});
}
另一种方法是使用finallyObservable 对象的方法。
ngOnInit() {
this.psService.tDate
.finally(() => {
this.filterData = [...new Set(this.cachedResults.map(item => item.cus_name))].filter(Boolean);
})
.subscribe(x => {
this.cachedResults = x;
});
}
在这里,该finally方法在 observable 完成后或发生错误时调用。
添加回答
举报