gpt4 book ai didi

json - 类型 Undefined 不可分配给类型 [Film]

转载 作者:可可西里 更新时间:2023-11-01 16:51:22 26 4
gpt4 key购买 nike

我正在学习如何使用服务以 Angular 方式发出 http 请求,这是我的代码:

export class ApiCallService {
// DECLARATIONS:
tmdb_api_key = '*******'; // personal api key to access the TMDB API
posterBaseAddress = 'https://image.tmdb.org/t/p/w300'; // base
address of the TMDB poster link, to add film specific path
// SEARCH PARAMETERS
requestPages: number; // get the number of pages for the request so is possible to load all the films (request is 1 page at time)
pageToLoad = 1; // number of page to load, start with 1
baseLanguage = 'en-US'; // return film in english, search can be done in local language but return english titles
foundFilms: [Film] = [];

constructor(private http: Http) {}
// TODO: GET method to search for film

getFilmsByTitle(filmTitle: string, page: number) {
// if the search has only 1 page will be 1 otherwise will load the films on the respective page
this.pageToLoad = page;
return this.http.get('https://api.themoviedb.org/3/search/movie?' +
'api_key=*******$' +
'&language=' + this.baseLanguage +
'&query=' + filmTitle +
'&page=' + this.pageToLoad +
'&include_adult=false')
.map(
(response: Response) => {
const films = response.json();
for (const film of films['results']) {
const singleFilm = new Film(film.title, film.overview, film.release_date, film.poster_path, film.genre_ids);
this.foundFilms.push(singleFilm);
}
this.requestPages = films.total_pages;
return this.foundFilms;
}
);
}
}


export class AppComponent implements OnInit {

constructor(private apiCallService: ApiCallService) {}

ngOnInit() {
this.apiCallService.getFilmsByTitle('Harry', 1).subscribe(
(films: [any]) => {
console.log(films);
}
);
}
}

当我启动应用程序时出现此错误,src/app/services/apicall.service.ts(15,5) 中的错误:错误 TS2322:类型“undefined[]”不可分配给类型“[电影]'。 “undefined[]”类型中缺少属性“0”。

并且在控制台中出现此错误:无法读取未定义的属性“推送”

我不明白为什么,我尝试了几个来声明 foundFilms 数组,但我无法让它工作。

希望有人能帮忙

谢谢亚历山德罗

最佳答案

声明时需要初始化数组。

foundFilms: 电影[] = [];

关于json - 类型 Undefined 不可分配给类型 [Film],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com