Skip to content Skip to sidebar Skip to footer

Cannot Read Property "search" Of Undefined

I'm trying to make a script work that uses theYoutube API, i put in a keyword, youtube api finds video -> script takes first result and returns VideoID. Now my problem is that t

Solution 1:

I made a few changes in the JS and i added a field in the html to display the video id.

The html file:

<scriptsrc="search.js"type="text/javascript"></script><scriptsrc="https://apis.google.com/js/client.js?onload=onClientLoad"type="text/javascript"></script><body><center><h3class="h3">KJKerstborrel - Muziekrequest</h3><divclass="input"><formname="muziek"action="succes/index"method="post"><inputtype="text"class="input-xlarge"id="artiest"name="artiest"placeholder="Gewenste artiest" /><br><inputtype="text"class="input-xlarge"id="nummer"name="nummer"placeholder="Gewenst nummer"required/><br><buttonstyle="width: 200px;"class="btn btn-success"onClick="search()"type="button">Deze wil ik horen!</button><br><inputtype="text"class="input-xlarge"id="VideoURL"name="VideoURL"placeholder="VideoURL"/><br></form></div></center></body>

The JS file:

// Your use of the YouTube API must comply with the Terms of Service:// https://developers.google.com/youtube/termsvar YT = 'undefined';

// Helper function to display JavaScript value on HTML page.functionshowResponse(response) {
    YT = response;
    // changed: namegiving
    document.getElementById('VideoURL').value = YT.items[0].id.videoId;
}

// Called automatically when JavaScript client library is loaded.functiononClientLoad() {
    gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
    //search();    // changed.
}

// Called automatically when YouTube API interface is loaded (see line 9).functiononYouTubeApiLoad() {
    // This API key is intended for use only in this lesson.// See http://goo.gl/PdPA1 to get a key for your own applications.
    gapi.client.setApiKey('AIzaSyD49-XZ2JV7Rws3KDM2T7nA56Jbi-O7djY');
}

functionsearch() {
    // Use the JavaScript client library to create a search.list() API call.var qVar = document.getElementById("artiest").value
             + " - "
             + document.getElementById("nummer").value;
    // changed. added: typevar request = gapi.client.youtube.search.list({
        type: 'video',
        part: 'id',
        q: qVar
    });

    // Send the request to the API server,// and invoke onSearchRepsonse() with the response.
    request.execute(onSearchResponse);
}

// Called automatically with the response of the YouTube API request.functiononSearchResponse(response) {
    showResponse(response);
}

Solution 2:

i followed your example and i got the same error but then followed this tutorial and i was able to make a successful call

https://developers.google.com/api-client-library/javascript/samples/samples

Post a Comment for "Cannot Read Property "search" Of Undefined"