javascript - 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));
}
解决方案
您应该检查是否有电影,测试...
<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>
推荐阅读
- python - python中的时间和随机模块
- java - Java:从 jUnit 调用主要实现的接口方法 - 测试
- javascript - 根据预先选择的下拉值隐藏和显示文件输入
- python - 为内容提供迭代器的类
- python - 如何在 Ubuntu 上安装 pgadmin4
- java - 在 codility 样本测试中仅管理 77%
- java - 获取 titledPane JAVAFX 中复选框的选中值
- python - Python Spyder 控制台:选择代码并在括号、括号、引号之间放置...(如在 RStudio 中)
- python - 如何使用 Beautifulsoup 从表中提取数据,没有文本
- visual-studio-code - 如何在 Python 3.7 上安装 TensorFlow