Angular 我不知道';I don’我不知道为什么我会收到这样一条信息:“我不知道。”;错误类型错误:无法设置属性';位置';“未定义”的定义;

Angular 我不知道';I don’我不知道为什么我会收到这样一条信息:“我不知道。”;错误类型错误:无法设置属性';位置';“未定义”的定义;,angular,typeerror,Angular,Typeerror,我在一个角度组件中有一个JSON数组,我想把它移到另一个数组中 类型声明: 测量详情: export interface MeasurementDetails { _id: string; measureTitle: string; measureDescription: string; measureSymbol: string; } 和测量对等项: export interface MeasureTypeElemetns { title: string; posit

我在一个角度组件中有一个JSON数组,我想把它移到另一个数组中

类型声明:

测量详情:

export interface MeasurementDetails {
  _id: string;
  measureTitle: string;
  measureDescription: string;
  measureSymbol: string;
}
和测量对等项:

export interface MeasureTypeElemetns {
  title: string;
  position: number;
  description: string;
  symbol: string;
}
我声明了两个初始化如下的数组:

mp: MeasurementDetails[] = [];
ELEMENT_DATA: MeasureTypeElemetns[] = [];
我请求了一个HTTP请求,它响应了我。 我在组件类的构造函数中调用它,如下所示:

  this.mpserv.getall().subscribe(
    x => {
      x.forEach(elementj => {
        this.mp.push(elementj);
      });
      this.filltoelements(this.mp);
    },
    err => console.error('Observer got an error: ' + err),
    () => console.log('Observer got a complete notification')
  );
filltoelements(mdata: MeasurementDetails[]) {
  if (!mdata) {
    console.log('data is undefined!!!!');
  } else {
    let i = -1;
    mdata.forEach(elementi => {
      i++;
      this.ELEMENT_DATA[i].position = (i + 1);
      this.ELEMENT_DATA[i].title = elementi.measureTitle;
      this.ELEMENT_DATA[i].description = elementi.measureDescription;
      this.ELEMENT_DATA[i].symbol = elementi.measureSymbol;
    });
  }
}
“filltoelements”函数声明如下:

  this.mpserv.getall().subscribe(
    x => {
      x.forEach(elementj => {
        this.mp.push(elementj);
      });
      this.filltoelements(this.mp);
    },
    err => console.error('Observer got an error: ' + err),
    () => console.log('Observer got a complete notification')
  );
filltoelements(mdata: MeasurementDetails[]) {
  if (!mdata) {
    console.log('data is undefined!!!!');
  } else {
    let i = -1;
    mdata.forEach(elementi => {
      i++;
      this.ELEMENT_DATA[i].position = (i + 1);
      this.ELEMENT_DATA[i].title = elementi.measureTitle;
      this.ELEMENT_DATA[i].description = elementi.measureDescription;
      this.ELEMENT_DATA[i].symbol = elementi.measureSymbol;
    });
  }
}
但当我运行时,它会显示一条消息:

"ERROR TypeError: Cannot set property 'position' of undefined
     at measure-type.component.ts:74
     at Array.forEach (<anonymous>)
     at MeasureTypeComponent.push../src/app/cc/measurement/measure- 
 type/measure-type.component.ts.MeasureTypeComponent.filltoelements"
“错误类型错误:无法设置未定义的属性“位置”
at测量类型。组件。ts:74
在Array.forEach()处
在MeasureTypeComponent.push../src/app/cc/measurement/measurement-
类型/度量值类型.component.ts.MeasureTypeComponent.filltoelements“

我认为这与我的'ELEMENT_DATA'数组的初始化有关。但我不知道如何解决它。

元素\u数据
是空数组,因此它返回
未定义的
元素

在访问其属性之前,只需分配一个空对象,如
this.ELEMENT_DATA[i]={}

此代码将起作用

filltoelements(mdata:MeasurementDetails[]){
如果(!mdata){
log('数据未定义!!!!');
}否则{
设i=-1;
mdata.forEach(elementi=>{
i++;
此.ELEMENT_DATA[i]={}作为度量对等项;
this.ELEMENT_DATA[i].位置=(i+1);
this.ELEMENT_DATA[i].title=elementi.measureTitle;
this.ELEMENT_DATA[i].description=elementi.measureDescription;
this.ELEMENT_DATA[i].symbol=elementi.measureSymbol;
});
}
}

它捕获到一个错误:类型“{}”缺少类型“MeasureTypeElements”中的以下属性:标题、位置、描述、符号螺栓(2739)我使用了这个而不是{}:这个。元素_数据[I]={title:'',位置:0,描述:'',符号:'};这是打字问题。将代码更改为
{}作为MeasureTypeelements
。我已经更新了我的帖子。