Youtube Data API - Uncaught TypeError: Cannot read

2019-09-19 10:54发布

问题:

Hi I'm trying to get the Search part of the Youtube Data API to work but not having much luck.

I completely copied the code from Youtube's GitHub page whilst following their tutorial

YT Github Page

The problem seems to be with this function;

function search() {
  var q = $('#query').val();
  var request = gapi.client.youtube.search.list({
    q: q,
    part: 'snippet'
  });

  request.execute(function(response) {
    var str = JSON.stringify(response.result);
    $('#search-container').html('<pre>' + str + '</pre>');
  });
}

Here's the test site

Any help would be much appreciated

Thanks ;)

回答1:

There is a error in your search function, which does not let the JS load initially and which later gives you the undefined error. Following is the correct source code: https://jsfiddle.net/aqwpg8ef/

 request = gapi.client.youtube.search.list({
    q: 'q', //old one was without quotes
    part: 'id, snippet', //the parts should be in quotes as well
    type: 'video',
    order: 'date'
 });

Mostly, on ajax call for search. near lines 20-22

Hope that helps!

Cheers..