Javascript 错误类型错误:无法读取未定义的属性X
我将以下接口用作JSON文件的类型:Javascript 错误类型错误:无法读取未定义的属性X,javascript,json,angular,typescript,mean-stack,Javascript,Json,Angular,Typescript,Mean Stack,我将以下接口用作JSON文件的类型: 导出接口IIindustrySector{ 行业部门:字符串; 选择:字符串; dataSubjectCategories:字符串[]; dataTypeCategories:字符串[]; 子行业:[{ 行业部门:字符串; 选择:字符串; dataSubjectCategories:字符串[]; dataTypeCategories:字符串[]; 子行业:[{}] }] }筛选方法未找到匹配项。因此,可观测对象产生了一个未定义的 getBySector(s
导出接口IIindustrySector{
行业部门:字符串;
选择:字符串;
dataSubjectCategories:字符串[];
dataTypeCategories:字符串[];
子行业:[{
行业部门:字符串;
选择:字符串;
dataSubjectCategories:字符串[];
dataTypeCategories:字符串[];
子行业:[{}]
}]
}
筛选方法未找到匹配项。因此,可观测对象产生了一个未定义的
getBySector(sector): Observable<IIndustrySectors| undefined> {
return this.getMainIndustrySectors().pipe(
map((products: IIndustrySectors[]) => products.find(p => p.IndustrySector === sector)));
// ^^^ matches none
}
getBySector(扇区):可观察{
返回此.getMainIndustrySectors()管道(
map((产品:iIndustrySector[])=>products.find(p=>p.IndustrySector===sector));
//^^^与无匹配
}
筛选方法未找到匹配项。因此,可观测对象产生了一个未定义的
getBySector(sector): Observable<IIndustrySectors| undefined> {
return this.getMainIndustrySectors().pipe(
map((products: IIndustrySectors[]) => products.find(p => p.IndustrySector === sector)));
// ^^^ matches none
}
getBySector(扇区):可观察{
返回此.getMainIndustrySectors()管道(
map((产品:iIndustrySector[])=>products.find(p=>p.IndustrySector===sector));
//^^^与无匹配
}
在您的getBySector
服务中,您会说:
products.find(p => p.IndustrySector === sector))
如果数组中没有与选择器匹配的对象,则使用将返回undefined
,在这种情况下,如果没有产品具有IndustrySector===sector
。这就是为什么要求服务的返回类型为可观察
如果您在编译时看到此错误,或者在IDE中看到此错误,则是由于此返回类型所致;它知道响应可能是未定义的,因此,您必须考虑这种可能性。将消费代码更改为以下应该可以解决此问题:
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])
}
}
}
})
this.readjsonService.getBySector(扇区).subscribe(响应=>{
如果(响应!==未定义){
如果(response.dataSubjectCategories.length>0){
for(设i=0;i
但是请注意这意味着,在运行时,当传入的扇区
与任何产品都不匹配时,将不会在getBySector
服务中执行for
循环,您可以说:
products.find(p => p.IndustrySector === sector))
如果数组中没有与选择器匹配的对象,则使用将返回undefined
,在这种情况下,如果没有产品具有IndustrySector===sector
。这就是为什么要求服务的返回类型为可观察
如果您在编译时看到此错误,或者在IDE中看到此错误,则是由于此返回类型所致;它知道响应可能是未定义的,因此,您必须考虑这种可能性。将消费代码更改为以下应该可以解决此问题:
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])
}
}
}
})
this.readjsonService.getBySector(扇区).subscribe(响应=>{
如果(响应!==未定义){
如果(response.dataSubjectCategories.length>0){
for(设i=0;i
但是请注意这意味着,在运行时,当传入的扇区
与任何产品都不匹配时,将不会执行for
循环
TypeError:无法读取未定义的属性“dataSubjectCategories”
如上所述,您试图访问未定义对象的dataSubjectCategories
。因此,response
不是对象
使用响应[0]。dataSubjectCategories
而不是响应。dataSubjectCategories
演示
var响应=[
{
“工业部门”:“住宿和餐饮服务活动”,
“isSelected”:“false”,
“dataSubjectCategories”:[“DS.Employees”,“DS.Collaborators”],
“数据类型类别”:“个人数据”,
“子行业”:[
{
“行业部门”:“餐饮服务活动”,
“isSelected”:“false”,
“dataSubjectCategories”:[],
“dataTypeCategories”:[],
“子行业”:[]
},
{
“工业部门”:“住宿”,
“isSelected”:“false”,
“数据主体类别”:[“未成年人”、“父母”、“法人”、“自然人”、“残疾人士”],
“dataTypeCategories”:[],
“子行业”:[]
}
]
}
];
var dsofsectors=[];
if(响应[0].dataSubjectCategories.length>0){
对于(i=0;i
TypeError:无法读取未定义的属性“dataSubjectCategories”
如上所述,您试图访问未定义对象的dataSubjectCategories
。因此,response
不是对象
使用响应[0]。dataSubjectCategories
而不是响应。dataSubjectCategories
演示
var响应=[
{
“工业部门”:“住宿和餐饮服务活动”,
“isSelected”:“false”,
“dataSubjectCategories”:[“DS.Employees”,“DS.Collaborators”],
“数据类型类别”:“个人数据”,
“子行业”:[
{
“行业部门”:“餐饮服务活动”,
“isSelected”:“false”,
“dataSubjectCategories”:[],
“dataTypeCategories”:[],
“子行业”:[]
},
{
“工业部门”:“住宿”,
“isSelected”:“false”,
“数据主体类别”:[“未成年人”、“父母”、“法人”、“自然人”、“残疾人士”],
“dataTypeCategories”:[],
“子行业”:[]
}
]
}
];
var dsofsectors=[];
if(响应[0].dataSubjectCategories.length>0){
为了=