[英]ERROR TypeError: Cannot read property X of undefined
I have following Interface to be used as a type for a JSON file : 我有以下接口可用作JSON文件的类型:
export interface IIndustrySectors { IndustrySector: string; isSelected: string; dataSubjectCategories:string[]; dataTypeCategories:string[]; SubIndustries:[{ IndustrySector: string; isSelected: string; dataSubjectCategories:string[]; dataTypeCategories:string[]; SubIndustries:[{}] }] }
and, two services: 并且,两项服务:
// Read all the Data from the JSON file getMainIndustrySectors(): Observable<IIndustrySectors[]> { return this.http.get<IIndustrySectors[]>(this.industrySectorsURL).pipe( tap(((data) => console.log('All: ' + data) )), catchError(this.handleError) ); } //Get Specific object getBySector(sector): Observable<IIndustrySectors| undefined> { return this.getMainIndustrySectors().pipe( map((products: IIndustrySectors[]) => products.find(p => p.IndustrySector === sector))); }
Here is a part of the JSON file 这是JSON文件的一部分
[ { "IndustrySector":"Accommodation and Food Service Activities", "isSelected": "false", "dataSubjectCategories":["DS.Employees","DS.Collaborators"], "dataTypeCategories":"Personal Data", "SubIndustries": [ { "IndustrySector":"Food and Beverage Service Activities", "isSelected": "false", "dataSubjectCategories":[], "dataTypeCategories":[], "SubIndustries":[] }, { "IndustrySector":"Accommodation", "isSelected": "false", "dataSubjectCategories":["DS.Minor","DS.Parent","DS.Legal Person","DS.Natural Person","DS.Disable"], "dataTypeCategories":[], "SubIndustries":[] } ] } ]
The problem is when I call the service "getBySector" by following code: 问题是当我通过以下代码调用服务“ getBySector”时:
this.readjsonService.getBySector(sector).subscribe(response=>{ if(response.dataSubjectCategories.length>0) { for(i=0; i<response.dataSubjectCategories.length;i++ ){ this.DSofSectores.push(response.dataSubjectCategories[i]) } } })
It tthrows an error: 它抛出一个错误:
TypeError: Cannot read property 'dataSubjectCategories' of undefined " TypeError:无法读取未定义的属性“ dataSubjectCategories”
it prints the values, though. 不过,它会打印值。
Why is it? 为什么?
what do is that based on the sector, I get in "response" other data related to it and fill an array of type string which are bind to dropdown list. 基于该扇区的操作是,我“响应”与之相关的其他数据,并填充一个绑定到下拉列表的字符串类型数组。 It works fine, but the following image showes what happens after selecting the sub sector: 它工作正常,但下图显示了选择子扇区后发生的情况:
PLEASE help me , I am newbie and I am so sick of this :(( Thanks. 请帮助我,我是新手,对此我感到厌烦:(((谢谢。
EDIT: when I say 编辑:当我说
if (response == undefined) { throw new Error("sector not found"); } else { .....
it passes the condition, meaning it is not undefined, yet it say "cannot read undefined" 它通过条件,表示它不是未定义的,但是它说“无法读取未定义的”
The filter method does not find a match. 筛选方法找不到匹配项。 So the observable is yielding a undefined
. 因此,可观察值产生一个undefined
。
getBySector(sector): Observable<IIndustrySectors| undefined> {
return this.getMainIndustrySectors().pipe(
map((products: IIndustrySectors[]) => products.find(p => p.IndustrySector === sector)));
// ^^^ matches none
}
In your getBySector
service, you say: 在您的getBySector
服务中,您说:
products.find(p => p.IndustrySector === sector))
Using Array#find
will return undefined
if no objects in the array match the selector, in this case if no products have IndustrySector === sector
. 如果Array#find
没有对象与选择器匹配,则使用Array#find
将返回undefined
,在这种情况下,如果没有产品具有IndustrySector === sector
。 This is why the service is required to have a return type of Observable<IIndustrySectors|undefined>
. 这就是为什么要求服务的返回类型为Observable<IIndustrySectors|undefined>
。
If you're seeing this error at compile-time, or as an error in your IDE, it's because of this return type; 如果您在编译时看到此错误,或者在IDE中看到此错误,则是由于此返回类型引起的。 it knows that the response can be undefined
and therefore, you have to account for that possibility. 它知道响应可能是undefined
,因此您必须考虑这种可能性。 Changing your consuming code to the following should fix the problem: 将使用方代码更改为以下内容可以解决此问题:
this.readjsonService.getBySector(sector).subscribe(response => {
if (response !== undefined) {
if(response.dataSubjectCategories.length > 0) {
for(let i = 0; i < response.dataSubjectCategories.length; i++) {
this.DSofSectores.push(response.dataSubjectCategories[i])
}
}
}
})
But be aware that this means that, at runtime, when a sector
is passed in that doesn't match any products, the for
loop will not be executed . 但是请注意 ,这意味着在运行时,如果传入的sector
与任何产品都不匹配, 则不会执行 for
循环。
TypeError: Cannot read property 'dataSubjectCategories' of undefined " TypeError:无法读取未定义的属性“ dataSubjectCategories”
As above statement suggest that you are trying to access dataSubjectCategories
of undefined object. 如上所述,建议您尝试访问未定义对象的dataSubjectCategories
。 Hence, response
is not an object. 因此, response
不是目标。
Use response[0].dataSubjectCategories
instead of response.dataSubjectCategories
使用response[0].dataSubjectCategories
而不是response.dataSubjectCategories
Demo 演示版
var response = [ { "IndustrySector":"Accommodation and Food Service Activities", "isSelected": "false", "dataSubjectCategories":["DS.Employees","DS.Collaborators"], "dataTypeCategories":"Personal Data", "SubIndustries": [ { "IndustrySector":"Food and Beverage Service Activities", "isSelected": "false", "dataSubjectCategories":[], "dataTypeCategories":[], "SubIndustries":[] }, { "IndustrySector":"Accommodation", "isSelected": "false", "dataSubjectCategories":["DS.Minor","DS.Parent","DS.Legal Person","DS.Natural Person","DS.Disable"], "dataTypeCategories":[], "SubIndustries":[] } ] } ]; var DSofSectores = []; if(response[0].dataSubjectCategories.length>0) { for(i=0; i<response[0].dataSubjectCategories.length;i++ ) { DSofSectores.push(response[0].dataSubjectCategories[i]) } } console.log(DSofSectores);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.