问题
After updating my project to use the HttpClient
module instead of the Http
module, the following no longer works.
The problem is Property json does not exist on type object
. I do need to get the items
property. How can I achieve this?
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;
}
回答1:
You don't need to use .json() with HttpClient
as the response itself already a json. change it as follows,
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/map-json-with-angularhttpclient