错误TypeError:无论我尝试过什么,都无法读取未定义的属性“路径”

时间:2019-02-25 11:07:14

标签: angular angular7

无论尝试什么,我都会不断收到未定义的错误。当console.log(this.EmployeeDetails.gallery);gallery[i].path确实有值时,我确实有值,但是我的浏览器控制台不断提示它,我尝试了各种方法,例如首先设置空字符串,在另一个函数中移动for循环,等待它,一切都不起作用。

await this.employeeService.getEmployeeDetails(this.Employeeuuid).subscribe((res) => {
  this.EmployeeDetails = res as Employee[];
  this.data.serviceprice = this.EmployeeDetails.serviceprice;
  this.setEmployees();
  console.log(this.EmployeeDetails.gallery);

  for (let i = 0; i <= this.EmployeeDetails.gallery.length; i++) {
    this.photossrc = this.EmployeeDetails.gallery[i].path;
    const src = this.photossrc;
    const caption = 'Image ' + i + ' caption here';
    const thumb = this.EmployeeDetails.gallery[i].path;
    const album = {
      src: src,
      caption: caption,
      thumb: thumb
    };
    this.albums.push(album);
  }
});
  

错误TypeError:无法读取未定义的属性“路径”       在EmployeeEditComponent.push ../ src / app / pages / employee / employee-edit / employee-edit.component.ts.EmployeeEditComponent.loadPhotosAndVideos中   (employee-edit.component.ts:201)       在SafeSubscriber._next(employee-edit.component.ts:96)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:196)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber.next   (Subscriber.js:134)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._next   (Subscriber.js:77)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next   (Subscriber.js:54)       在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / map.js.MapSubscriber._next中   (map.js:41)       在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next中   (Subscriber.js:54)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / filter.js.FilterSubscriber._next下   (filter.js:38)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next中   (Subscriber.js:54)

1 个答案:

答案 0 :(得分:1)

循环长度不应该小于或等于刚刚变短的长度,例如:

 for (let i = 0; i < this.EmployeeDetails.gallery.length; i++) {
     ...
    }