I'm trying to use the new HttpClient class instead of the old Http.
I want to map over the data I get from the subscribe method, but get the below error. Any suggestions on why I get this?
Code:
export class YoutubeSearchService {
constructor(
private http: HttpClient,
@Inject(YOUTUBE_API_KEY) private apiKey: string,
@Inject(YOUTUBE_API_URL) private apiUrl: string,
) { }
search(query: string): Observable<SearchResult[]> {
const params: string = [
`q=${query}`,
`key=${this.apiKey}`,
`part=snippet`,
`type=video`,
`maxResults=10`,
].join("&");
const queryUrl = `${this.apiUrl}?${params}`;
return this.http.get(queryUrl).subscribe(data => {
data.map(item => {
return new SearchResult({
id: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
thumbnailUrl: item.snippet.thumbnails.high.url,
});
});
});
}
}
Error:
ERROR in src/app/services/youtube-search.service.ts(26,5): error TS2322: Type 'Subscription' is not assignable to type 'Observable<SearchResult[]>'.
Property '_isScalar' is missing in type 'Subscription'.
src/app/services/youtube-search.service.ts(27,12): error TS2339: Property 'map' doesnot exist on type 'Object'.
Your search
method is returning a subscription
but the signature claims it should return an Observable<SearchResult[]>
To fix this either change the signature of your method or change the subscribe
to map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With