在更新我的项目以使用HttpClient模块而不是Http模块之后,下面的内容就不再有效了。
问题是Property json does not exist on type object。我确实需要获得items属性。我怎样才能做到这一点?
private loadLatestVideosForChannelId( channelId: string ): Promise<any[]> {
// load videos from youtube-data-api
let videos = this.http.get(
'https://www.googleapis.com/youtube/v3/search' +
'?key=' + this.apiKey +
'&channelId=' + channelId +
'&part=snippet,id' +
'&order=date' +
'&type=video' +
'&maxResults=3'
)
.pipe(
// if success
map( res => {
return res.json()['items']; // the problem
}),
// if error
catchError( (err) => {
console.log( "YouTube API Error > Cannot get videos for this channel :(" )
return null;
}),
take(1)
)
.toPromise() as Promise<any[]>;
return videos;
}发布于 2019-01-27 04:07:21
您不需要在HttpClient中使用.json()作为响应本身已经是一个json。修改如下,
this.http.get(
'https://www.googleapis.com/youtube/v3/search' +
'?key=' + this.apiKey +
'&channelId=' + channelId +
'&part=snippet,id' +
'&order=date' +
'&type=video' +
'&maxResults=3'
)
.pipe(
map((res: any) => {
return res['items'];
})
);
https://stackoverflow.com/questions/54384961
复制相似问题