【发布时间】:2019-04-17 07:21:24
【问题描述】:
我有组件,我在其中显示数据,从后端获取。
这是显示数据的功能
getData() {
this.propertyService.getPropertyForEdit(Number(this.activatedRoute.snapshot.params['id'])).subscribe(r => {
this.property = r;
this.tabTemplates = [
{ title: this.l('Addresses'), template: this.addressesTemplateRef },
{ title: this.l('Site'), template: this.siteTemplateRef}
];
this.propertiesToDisplay = [
{ displayName: this.l('PropertyTitle'), value: this.property.propertyTitle.name},
{ displayName: this.l('Landlord'), value: this.property.landlord.name},
{ displayName: this.l('Agent'), value: this.property.agent.name },
{ displayName: this.l('BuildingType'), value: this.property.buildingType.name }
];
});
}
在这一行中value: this.property.landlord.name} 的值可以为空。
所以当它为空时,我有错误
无法读取未定义的属性“名称”
我该如何解决这个问题?
【问题讨论】:
-
this.property.landlord.name || "N/A"试试这个 -
this.property.landlord未定义。您应该使用 fallbacks 检查是否存在房东。(this.property && this.property.landlord && this.property.landlord.name) ? this.property.landlord.name : 'N/A' -
或使用 .map 并对所有未定义的值执行相同操作。
标签: javascript typescript object