title: ERROR TypeError: Cannot read property 'length' of undefined angular 8
body: <p>I made a site to pick a cat at random like Cat and Mash, but I have an error that I cannot understand.</p>
  
  <p>I have a JSON object in which there are urls that contain images. I need to display the images randomly but not 2 times the same image.</p>
  
  <p>Console:</p>
  
  <p><a href="https://i.stack.imgur.com/QyGBW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QyGBW.jpg" alt="enter image description here"></a></p>
  
  <p>Why is <code>length</code> undefined?</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { CatService } from '../services/cat.service';
  import { CatList, Cat } from '../model/cat';
  
  @Component({
    selector: 'app-cat',
    templateUrl: './cat.component.html',
    styleUrls: ['./cat.component.css']
  })
  export class CatComponent implements OnInit {
      twoCatsArray: Cat[] = [];
      allcats: Cat[];
      constructor(private catService: CatService) {}
      ngOnInit() {
          this.showMeTwoCats();
      }
      showMeTwoCats() {
          this.catService.getCats().subscribe((cats: CatList) = &gt; {
              this.allcats = cats.images;
              this.twoCatsArray = this.chooseTwoRandomCats(this.allcats);
          });
      }
      chooseTwoRandomCats(cats: Cat[]): Cat[] {
          const firstCatIndex = this.getRandomIndex(cats.length);
          const secondCatIndex = this.getRandomIndex(cats.length, firstCatIndex);
          return [cats[firstCatIndex], cats[secondCatIndex]];
      }
      getRandomIndex(maxValue: number, differentThanValue ? : number): number {
          let index: number;
          do {
              index = this.getRandomInt(maxValue);
          } while (index === differentThanValue);
          return index;
      }
      getRandomInt(max): number {
          return Math.floor(Math.random() * Math.floor(max));
      }
      voteForThisCat(id: string) {
          const likedCatindex = this.allcats.findIndex((cat: Cat) = &gt; cat.id === id);
          const newRating = this.getIncrementedCatRatingValue(this.catService.catVote[likedCatindex].rating);
          this.catService.catVote[likedCatindex].rating = newRating;
          this.twoCatsArray = this.chooseTwoRandomCats(this.allcats);
      }
      getIncrementedCatRatingValue(rating: number | undefined): number {
          return rating ? ++rating : 1;
      }
  }
  </code></pre>