首页 > 解决方案 > Angular无法读取未定义的属性“结果”

问题描述

我的代码运行良好,但没有控制台日志给出这些错误。无法results从不确定的属性中读取。有人能告诉我它是什么吗?我究竟做错了什么?此应用程序正在向 TMDB api 发出请求

MODEL ANGULAR

export interface Movies {
  results: (ResultsEntity)[];
  page: number;
  total_results: number;
  dates: Dates;
  total_pages: number;
}

export interface ResultsEntity {
  popularity: number;
  vote_count: number;
  video: boolean;
  poster_path: string;
  id: number;
  adult: boolean;
  backdrop_path: string;
  original_language: string;
  original_title: string;
  genre_ids?: (number)[] | null;
  title: string;
  vote_average: number;
  overview: string;
  release_date: string;
}
export interface Dates {
  maximum: string;
  minimum: string;
}
method list angular 

<div class="img carousel-cell" *ngFor="let m of movies.results">  
            <img routerLink="edit/{{m.id}}" src="https://image.tmdb.org/t/p/w200{{ m?.poster_path }}" alt="{{ m.title }}"/>
          </div>
          
          
          
Service Angular

  subs: Subscription[] = []
  latest!: Movies;
  nowPlaying!: Movies;
  comedy!: Movies;
  horror!: Movies;
  animation!: Movies;
  documentary!: Movies;
  originals!: Movies;
  

  constructor(public movies: ServiceApiService ) {}

  ngOnInit(): void {
    this.subs.push(this.movies.getNowPlayingMovie().subscribe(res => this.nowPlaying  =res));
    this.subs.push(this.movies.getComedy().subscribe(res => this.comedy  =res));
    this.subs.push(this.movies.getHorror().subscribe(res => this.horror  =res));
    this.subs.push(this.movies.getAnimation().subscribe(res => this.animation  =res));
    this.subs.push(this.movies.getDocumentary().subscribe(res => this.documentary  =res));
    this.subs.push(this.movies.getOriginals().subscribe(res => this.originals  =res));
    
  }

标签: javascriptangularapi

解决方案


您应该检查是否有电影,测试...

<ng-container *ngIf="movies && movies.results.length > 0">
    <div class="img carousel-cell" *ngFor="let m of movies.results">
        <img routerLink="edit/{{m.id}}" src="https://image.tmdb.org/t/p/w200{{ m?.poster_path }}" alt="{{ m.title }}"/>
    </div>
</ng-container>

推荐阅读