错误TypeError:无法读取未定义的属性'nomEtatAvancement'

时间:2019-08-26 10:05:55

标签: angular typescript

我一直在与这段代码斗争,我得到了:

  

错误TypeError:无法读取未定义的属性'nomEtatAvancement'

etat-avancement.component.ts

export class EtatAvancementComponent {

  settings = {
    add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      nom_etat_avancement: {
        title: 'Nom D\'Etat D\'Avancement',
        type: 'string',
      },
      descri_avanacement: {
        title: 'Description Etat D\'Avancement',
        type: 'string',
      },
    },
  };

  source: LocalDataSource = new LocalDataSource();
  etat: any = [];

  constructor(private service: SmartTableData, public rest: RestService) {

    this.rest.getEtatAvancements().subscribe((data: any) => {
      let i = 0;
      for (let dataKey in data) {
        this.etat.push({
          "nom_etat_avancement": data["content"][i].nomEtatAvancement,
          "descri_avanacement": data["content"][i].descriAvanacement
        });
        i++;
      }
      console.log(this.etat);
      this.source.load(this.etat);
    });
  }

这是浏览器控制台中的角度服务的结果:

{links: Array(2), content: Array(1), page: {…}}
content: Array(1)
0: {nomEtatAvancement: "En Cours", descriAvanacement: "Ce projet est en cours de réalisation.", content: Array(0), links: Array(6)}
length: 1
__proto__: Array(0)
links: (2) [{…}, {…}]
page: {size: 20, totalElements: 1, totalPages: 1, number: 0}
__proto__: Object

这是正确的。

2 个答案:

答案 0 :(得分:0)

for (let dataKey in data)迭代data的属性:linkscontentpage,因此它超过了data.content的长度。

使用data.content.forEach(如果要在this.etat中保留以前的数据)或data.content.map(如果this.etat仅包含当前响应中的元素),如下面的代码段所示(将etat1etat2替换为this.etat

let data = {
  links: ["link1", "link2"],
  content: [{
    nomEtatAvancement: "En Cours",
    descriAvanacement: "Ce projet est en cours de réalisation.",
    content: [],
    links: [ /*some 6 links*/ ]
  }],
  page: {}
};

//you're doing this:
let i = 0;
for (let dataKey in data) {
  console.log("dataKey = ", dataKey, '; data["content"][i] =', data["content"][i]);
  i++;
}


//do this to append some values to existing array, leaving previous content
let etat1 = [];

data.content.forEach(content => {
  etat1.push({
    nom_etat_avancement: content.nomEtatAvancement,
    descri_avanacement: content.descriAvanacement
  })
})

console.log("etat1:", etat1);

//do this to replace your array with data from data.content
let etat2 = data.content.map(content => ({
    nom_etat_avancement: content.nomEtatAvancement,
    descri_avanacement: content.descriAvanacement
  })
)

console.log("etat2:", etat2);

答案 1 :(得分:0)

感谢您的帮助,我将代码更改为这样,并且效果很好:

source: LocalDataSource = new LocalDataSource();
  etat: any = [] ;

  constructor(private service: SmartTableData ,  public rest:RestService) {

      this.rest.getEtatAvancements().subscribe((data:any) =>{

     data.content.forEach((key: any , val: any)=>{
        this.etat.push({"nom_etat_avancement" : data["content"][val].nomEtatAvancement , "descri_avanacement" : data["content"][val].descriAvanacement});
      });
        this.source.load(this.etat);
    });