简体   繁体   English

类型错误:无法读取角度 6 中未定义的属性“长度”

[英]TypeError: Cannot read property 'length' of undefined in angular 6

I have not got any values from an array using the following TS code:我没有使用以下 TS 代码从数组中获得任何值:

this.dataservice.get("common/public/getAllCategories", null).subscribe(data => {
      //  //console.log('categories'+JSON.stringify( data));
   this.categoriesarray = data;
});
var data = this.items;
var categoriesdata = [];
for (var i = 0; i < data['categories'].length; i++) { // <- Error comes on this line
    categoriesdata.push(data['categories'][i].categoryID);
    this.selspecialization = categoriesdata;
}
this.EditRequest = 'block';   
}

My HTML code is:我的 HTML 代码是:

<button type="button" *ngIf="!userRechargeTable" class=" smallbtns btn roundButton btn-theme blue" (click)="edit()"> 
 Edit
</button>

It's hard to tell whether this is meant to be one block of code due to the mismatching variable / property names.由于变量/属性名称不匹配,很难判断这是否是一个代码块。 My guess is that you're trying to process the results of the http request outside of the observable - before the data service has returned the data.我的猜测是您正在尝试在 observable 之外处理 http 请求的结果 - 在数据服务返回数据之前。

What happens if you try the following:如果您尝试以下操作,会发生什么情况:

this.dataservice.get("common/public/getAllCategories", null).subscribe(data => {
  //  //console.log('categories'+JSON.stringify( data));
  this.categoriesarray = data;
  // this has been moved inside the observable, and also optimised using the map function
  this.selspecialization = data['categories'].map(x => x.categoryID);
  this.EditRequest = 'block';
});

I've taken the following steps:我采取了以下步骤:

  1. Move code to inside the subscribe().将代码移动到 subscribe() 内部。 This is where code should run after the observable (http request in this case) returns a value.这是在 observable(在这种情况下为 http 请求)返回值后代码应该运行的地方。

  2. Compress your loop into a single map function.将您的循环压缩为单个 map 函数。 Your original for loop was unecessary and also performing an unecessary assignment to this.selspecialization each time.您原来的for循环是不必要的,并且每次都对this.selspecialization执行不必要的分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError无法读取未定义的属性“ length”-角度4 - TypeError cannot read property “length” of undefined - angular 4 Angular 8“类型错误:无法读取未定义的属性‘长度’” - Angular 8 "TypeError: Cannot read property 'length' of undefined" TypeError:无法读取角度4中未定义的属性“ length” - TypeError: Cannot read property 'length' of undefined in angular 4 Angular 6 ng build无法读取未定义的Type的属性“ length”:无法读取levenshtein的未定义的属性“ length” - Angular 6 ng build Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined at levenshtein Angular 7 TypeError:无法读取 setTitle 处未定义的属性“长度” - Angular 7 TypeError: Cannot read property 'length' of undefined at setTitle 错误类型错误:无法读取未定义 angular 8 的属性“长度” - ERROR TypeError: Cannot read property 'length' of undefined angular 8 TypeError:无法读取Angular应用中未定义的属性“ length” - TypeError: Cannot read property 'length' of undefined in Angular app 错误TypeError:无法读取Angular 7 Drag and Drop中未定义的属性“length” - ERROR TypeError: Cannot read property 'length' of undefined in Angular 7 Drag and Drop Angular - 多选下拉 - TypeError:无法读取未定义的属性“长度” - Angular - multiselect-dropdown - TypeError: Cannot read property 'length' of undefined TypeError:无法读取未定义的属性“长度”(Angular 8)(标题) - TypeError: Cannot read property 'length' of undefined (Angular 8)(header)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM