【发布时间】:2021-10-22 19:28:15
【问题描述】:
我尝试用一个服务提供的值填充一个数组。
数组必须填充下拉列表。我关注这个post。
数组定义如下:
dropdownList: Array<{id: number, description: string}>;
服务方法是:
async loadFoodTypes(){
const api: string = environment.apiAddress + 'FoodType';
await this.httpClient.get<FoodType[]>(api)
.pipe()
.toPromise()
.then((response: FoodType[]) => {
this._foodType.next(response);
})
.catch(err => console.log(err))
}
FoodType 类型定义如下:
id?: number;
description: string;
shortDescription: string;
当我调用服务时,我尝试用数据填充数组。
loadFoodTypes(){
this.foodtypesService.loadFoodTypes().then(response =>
this.dropdownList.push({id: <number>(<unknown>response as FoodType).id, description: (<unknown>response as FoodType).description})
);
}
我的问题是浏览器控制台中有这两条错误消息:
Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'id')
和
Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'push')
我认为第二个链接到第一个。
感谢您的帮助。
【问题讨论】:
标签: javascript angular rxjs observable