Javascript 如何处理angular 5中未定义的对象属性

Javascript 如何处理angular 5中未定义的对象属性,javascript,angular,typescript,Javascript,Angular,Typescript,我试图通过将对象属性值设置为空字符串来修复未定义的错误,但仍然会出现如下所述的错误: 错误类型错误:无法读取未定义的属性“notes” TS let notes; if (typeof manufacturer_field.manufacturer_guidelines[0].notes == undefined){ notes = ''; } else { notes = manufacturer_field.manufacturer_guidelines[0].notes; } H

我试图通过将对象属性值设置为空字符串来修复未定义的错误,但仍然会出现如下所述的错误:

错误类型错误:无法读取未定义的属性“notes”

TS

let notes;
if (typeof manufacturer_field.manufacturer_guidelines[0].notes == undefined){
  notes = '';
} else {
  notes = manufacturer_field.manufacturer_guidelines[0].notes;
}
HTML

<p *ngIf="field.notes?.length">{{field.notes}}</p>
{{field.notes}


我引用了这个答案,但没有成功:

如果notes数组元素0未定义,那么这将抛出一个错误:

if (typeof manufacturer_field.manufacturer_guidelines[0].notes == undefined){
因为您正在检查属性是否未定义,而实际上未定义的是.manufacturer\u指南[0]项

相反,您可以执行以下操作:

if (!manufacturer_field.manufacturer_guidelines[0]){
     manufacturer_field.manufacturer_guidelines[0] = (assign it whatever value belong to this sort of item, and then once you know it is valid, then add notes)
}
此外,您将字符串指定给“notes”变量,而不是实际的数组项

假设我有一个汽车阵列:

if (!cars[0]) {
   cars[0] = <Car>{};
   cars[0].color = "blue";
}
if(!cars[0]){
cars[0]={};
汽车[0]。颜色=“蓝色”;
}

如果notes数组元素0未定义,则会引发错误:

if (typeof manufacturer_field.manufacturer_guidelines[0].notes == undefined){
因为您正在检查属性是否未定义,而实际上未定义的是.manufacturer\u指南[0]项

相反,您可以执行以下操作:

if (!manufacturer_field.manufacturer_guidelines[0]){
     manufacturer_field.manufacturer_guidelines[0] = (assign it whatever value belong to this sort of item, and then once you know it is valid, then add notes)
}
此外,您将字符串指定给“notes”变量,而不是实际的数组项

假设我有一个汽车阵列:

if (!cars[0]) {
   cars[0] = <Car>{};
   cars[0].color = "blue";
}
if(!cars[0]){
cars[0]={};
汽车[0]。颜色=“蓝色”;
}

问题不在于
注释
。这是关于
字段
和/或
制造商字段。制造商指南[0]
。问题不在于
注释
。关于
字段
和/或
制造商字段。制造商指南[0]