TypeError: res.json is not a function

Why is not a function? I’ve read this but I cannot make it work for my project, the error is shown in the following code

getListVideos(listId) {
    return this.http.get('https://www.googleapis.com/youtube/v3/playlistItems?key=' + this.apiKey + '&playlistId=' + listId +'&part=snippet,id&maxResults=20')
    .map((res: Response) => {
      return res.json()['items'];

Are you using HttpClient or Http module?

import { HttpClient } from '@angular/common/http'; and variable in constructor public http: HttpClient

HttpClient automatically imports json. Let’s say you are expecting to read an object of type User.

return this.http.get<User>(url).subscribe(user => console.log(user));
The variable user is of type User.

Without specifying a type, your response is automatically json. No need to parse it to json.


1 Like

good answer, it solved my problem in once. i just remove json from res.json and my other problems also got removed.