0

I've been trying to get a video duration with Youtube JavaScript API v3, this is the relevant part of my JS:

var request = gapi.client.youtube.search.list({
            q: 'eminem',
            part: 'snippet'
    });

    request.execute(function(response)  {                                                                                    
        $('#results').empty()
        var srchItems = response.result.items;     

        $.each(srchItems, function(index, item) {
            vidTitle = item.snippet.title;
            vidTime = item.contentDetails.duration;
            vidThumburl =  item.snippet.thumbnails.default.url;               
            $('#results').append('<pre>' + vidTitle + vidTime +'</pre>');                      
        });  
    });

The problem is in vidTime = item.contentDetails.duration; and console returns the following error:

Uncaught TypeError: Cannot read property 'duration' of undefined

But, i'm looking the JSON structure returned in the request (See Here) and it was to be working, and I don't know why i'm getting this error :(

I've tried to change the part to: part: snippet, contentDetails but with this change i've more errors...

So, how to do to fix this?

EDITED

To analyze the return of console.log(response); I could see that the contentDetails is not returned in the array, but then how can I get the video duration?

Igor
  • 645
  • 4
  • 16
  • 37
  • 1
    You are right that you have to include `contentDetails` in `part`. Can you post the errors that you get when extending `part` to read like `part: 'snippet,contentDetails'`? – sthzg Sep 11 '15 at 10:23
  • The following error: `Uncaught TypeError: Cannot read property 'items' of undefined` – Igor Sep 11 '15 at 17:10
  • 1
    Okay, that means that the `response` does not carry anything in the `result` attribute. To debug further, what does it show in the developer tools if you log `response`, i.e. `request.execute(function(response) { console.log(response); });` – sthzg Sep 11 '15 at 17:19
  • 1
    @sthzg I followed your tip and analyze the return on the console could see that the contentDetails is not returned in the array, but then how can I get the video duration? – Igor Sep 11 '15 at 17:31
  • 1
    Okay, then this answer seems to help further: http://stackoverflow.com/a/27415212/870769 – sthzg Sep 11 '15 at 17:42
  • Solved with http://stackoverflow.com/questions/27393842/contentdetails-or-duration-not-coming-using-youtube-v3-api/27415212#27415212 – Igor Sep 12 '15 at 13:37

1 Answers1

0

Your request only includes part: snippet. You have to also include contentDetails.

var request = gapi.client.youtube.search.list({
        q: 'eminem',
        part: 'snippet,contentDetails'
});
johnh10
  • 4,047
  • 1
  • 18
  • 30
  • thanks for the reply, but, if I add "contentDetails on my part I've the error: `Uncaught TypeError: Cannot read property 'items' of undefined` – Igor Sep 11 '15 at 17:10
  • I don't use gapi myself, but since you are trying to access item.contentDetails, you need to add contentDetails to your search parameter. – johnh10 Sep 12 '15 at 11:57