Devora
Devora

Reputation: 357

ERROR TypeError: Cannot read property 'path' of undefined no matter what i tried

I keep getting the undefined error no matter what I tried. I do have value when I console.log(this.EmployeeDetails.gallery);, gallery[i].path does have a value, but my browser console keep prompting it, I tried different methods like setting an empty string first, moving the for-loop in another function and set it await, everything doesn't work.

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);
  }
});

ERROR TypeError: Cannot read property 'path' of undefined at EmployeeEditComponent.push../src/app/pages/employee/employee-edit/employee-edit.component.ts.EmployeeEditComponent.loadPhotosAndVideos (employee-edit.component.ts:201) at SafeSubscriber._next (employee-edit.component.ts:96) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)

Upvotes: 0

Views: 628

Answers (1)

GaryB
GaryB

Reputation: 384

The loop should not be smaller than or equal to the length just smaller, like this:

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

Upvotes: 1

Related Questions