【发布时间】:2019-12-31 16:26:03
【问题描述】:
大家好,我有兴趣将喜欢的功能集成到我的项目中 所以我开始将 ionic 3 项目转换为 ionic 4 形式: https://github.com/SoatGroup/ionic3-movie-app 全文在这里: https://soat.developpez.com/tutoriels/mobiles/ionic-3-creer-application-mobile/
我收到了这个错误:
Client Error: TypeError: Cannot read property 'toString' of undefined
at FavoriteProviderService.getMovieKey (favorite-provider.service.ts:37)
at FavoriteProviderService.isFavoriteMovie (favorite-provider.service.ts:23)
我认为它在最喜欢的提供商内部:
getMovieKey(movie: IMovie) {
return MOVIE_KEY + movie.id.toString();}
我所做的唯一区别是使用 http 请求:
export class DetailsPage implements OnInit {
id: number;
movie: IMovie;
detailsData :IMovie;
isFavorite: boolean = false;
constructor(public BaseService : BaseService
,public activatedRoute: ActivatedRoute
,public router: Router
,private favoriteProviderService:FavoriteProviderService) { }
ngOnInit() {
this.id = this.activatedRoute.snapshot.params["id"];
this.BaseService.getDetails(this.id).subscribe(response => {
this.detailsData = response;
})
this.fav();
}//end ngOnInit
fav(){
this.movie =this.detailsData;
console.log("movie is:", this.movie);
this.favoriteProviderService.isFavoriteMovie(this.movie)
.then(value => (this.isFavorite = value));
}
toggleFavorite(): void {
this.isFavorite = !this.isFavorite;
this.favoriteProviderService.toogleFavoriteMovie(this.movie);
}
}
BaseService.ts:
getDetails(id): Observable<IMovie> {
return this.http
.get<IMovie>('my api here')
.pipe(
retry(2),
catchError(this.handleError)
)
}
这样回答:
[{id: 2,title: "somthing",image: "somthing"}]
我尝试在没有http请求的情况下进行检查,并完成了testData被放入存储
export class DetailsPage implements OnInit {
id: number;
//movie: IMovie;
//detailsData :IMovie;
isFavorite: boolean = false;
testData: IMovie = {
id: 2,
title: "somthing",
image: "somthing"
};
constructor(public BaseService : BaseService
,public activatedRoute: ActivatedRoute
,public router: Router
,private favoriteProviderService:FavoriteProviderService) { }
ngOnInit() {
/* this.id = this.activatedRoute.snapshot.params["id"];
this.BaseService.getDetails(this.id).subscribe(response => {
this.detailsData = response;
})*/
this.fav();
}//end ngOnInit
fav(){
//this.movie =this.detailsData;
//console.log("movie is:", this.movie);
this.favoriteProviderService.isFavoriteMovie(this.testData)
.then(value => (this.isFavorite = value));
}
toggleFavorite(): void {
this.isFavorite = !this.isFavorite;
this.favoriteProviderService.toogleFavoriteMovie(this.testData);
}
}
我需要帮助来找到解决方案,我真的很封闭,谢谢
【问题讨论】:
-
在你的 getMovieKey 函数中替换返回行,像这样
return MOVIE_KEY + (!!movie.id ? movie.id.toString() : "") -
如果它不起作用请告诉我
-
@kushalshah 是的,我已经在尝试这样的事情并且它可以工作,但是结果所有的细节电影看起来都被标记了,当你去最喜欢的页面时,你会看到第一个书签项目
-
@kushalshah 本地存储中的密钥必须是唯一的,因此我的示例中的 MOVIE_KEY 必须看起来像您的示例中的“movie_2”我得到了“movie_”
-
由于movie.id中没有值,调用getaMovieKey(movie)时请告知;在组件中?您将什么传递给参数?
标签: angular ionic-framework ionic4