Error: The variable YouTube has not been declared within this context / YouTube API

In my attempt to implement a search using the YouTube Data API, I have come up with the following code. The setup involves an express generated stack with jade.

#player

    script.
        // 2. This code loads the IFrame Player API code asynchronously.
        var tag = document.createElement('script');

        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // 3. This function creates an <iframe> (and YouTube player)
        //    after the API code downloads.
        var player;
        function onYouTubeIframeAPIReady() {
            player = new YT.Player('player', {
                height: '345',
                width: '540',
                videoId: 'M7lc1UVf-VE',
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
        }

        // 4. The API will call this function when the video player is ready.
        function onPlayerReady(event) {
            event.target.playVideo();
        }

        // 5. The API calls this function when the player's state changes.
        //    The function indicates that when playing a video (state=1),
        //    the player should play for six seconds and then stop.
        var done = false;
        function onPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING && !done) {
                setTimeout(stopVideo, 6000);
                done = true;
            }
        }

        function stopVideo() {
            player.stopVideo();
        }

        function googleApiClientReady() {
            gapi.client.setApiKey('AIzaSyCgOmTzCI4-gkUhL4hOm9R6I9tmUlVqtCw');
            gapi.client.load('youtube', 'v3', function() {
                /**
                * This function searches for videos related to the keyword 'dogs'. The video IDs and titles
                * of the search results are logged to Apps Script's log.
                *
                * Note that this sample limits the results to 25. To return more results, pass
                * additional parameters as documented here:
                *   https://developers.google.com/youtube/v3/docs/search/list
                */
                function searchByKeyword() {
                    var results = YouTube.Search.list('id,snippet', {q: 'dogs', maxResults: 25});
                    for(var i in results.items) {
                        var item = results.items[i];
                        console.log('[%s] Title: %s', item.id.videoId, item.snippet.title);
                    }
                }

                searchByKeyword();
            });
        }

    script(src="https://apis.google.com/js/client.js?onload=googleApiClientReady")`

Based on the code provided, the expected behavior is to load a video initially, followed by executing a search for 'dogs' and printing the results to the console.

Despite these intentions, an error message pops up:

ReferenceError: YouTube is not defined

The cause of this issue eludes me completely...perhaps something regarding the loading of the script...even after multiple attempts at placing it appropriately. Any guidance would be greatly appreciated.

UPDATE

I moved the script( back to the end - similar to how it was in the original code...and now I can confirm that the searchByKeyword method is indeed running. However, the problem has circled back to the initial YouTube is not defined problem. In the following excerpt, you'll find the output from my console.log message inserted at the start of the searchByKeyword method, along with the recurring error mentioned earlier:

searchByKeyword is running 
ReferenceError: YouTube is not defined

Answer №1

The main issue you are encountering is that gapi.client extends the base gapi Object when it loads, so in order to access the Youtube API, you should use gapi.client.youtube.search instead of Youtube.Search.

It's important to note that Javascript is inherently asynchronous, so any XHR request must return in a callback function or be promisified.

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

  request.execute(function(results) {
    for(var i in results.items) {
      var item = results.items[i];
      console.log('[%s] Title: %s', item.id.videoId,item.snippet.title);
    }
  });
 }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Vue Sortable functionality may be causing complications

Currently, I am utilizing vue-sortable and draggable. Although I am able to drag an item above and change its position, the JSON data does not refresh. Here is the existing list: <draggable v-model="this.$store.getters.getDocument.getDocumentAttributes ...

Hide the address bar in a Google Maps iFrame

After numerous attempts, I still can't seem to hide the address bar on this Google Maps iFrame. Can anyone provide a solution or workaround for this issue? https://i.sstatic.net/x0pl9.png I have tried using display:none; in our CSS, which successfull ...

Adding a break in a CardHeader element subtitle in MaterialUI using ReactJS

I have been working with older Material UI components (Version 0.20.1). Below is an excerpt of my code: return( <> <Card> <CardHeader actAsExpander={true} showExpandableButton={true} title = {user.name} ...

swap between style sheets glitching

My website features two stylesheets, one for day mode and one for night mode. There is an image on the site that triggers a function with an onclick event to switch between the two stylesheets. However, when new visitors click the button for the first ti ...

Using V-model binding in Vue always resets the content of text inputs

I am facing an issue with a Text Input that is filled based on a string stored in cookies, similar to a Remember me feature. When I bind the value of the input to the cookie using :value, I am unable to type a new value even if there is no cookie being sto ...

There is no information available at this time

Currently, I am delving into Angular and am keen on creating a web application that consumes a Restful Web service. The setup of my page is as follows: <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html ng-app="Tri ...

XMLHTTPRequest is experiencing issues with displaying the progress bar

I'm facing an issue with uploading images in PHP while showing the progress status. The image uploads correctly using XMLHttpRequest, but I can't see the progress bar moving. Below is my code. Can someone help me solve this problem? <html> ...

Issue with PHP's ng-click not functioning properly with Angular JS's dynamic HTML generation

I'm just starting out with Angular JS. I'm trying to create an ng-click event in dynamically generated HTML from PHP, here's the code snippet. var app = angular.module('pageapp',[]); angular.module('pageapp') .filter(& ...

Exploring Next.js: A Guide to Implementing Browsing History in Your Application

Struggling to add entries to browser history when using Next.js's Link property for page navigation. Unable to push history entry, leading to incorrect page location in my application when going back. Any ideas on implementing this feature in Next.js? ...

The visual representation of my data in Highchart appears sporadic, with the points scattered rather than following a clean, linear progression from left to right

I am working on displaying a 2D array [timestamp, count] on a highchart for the past 90 days from left to right. However, I am facing an issue where the chart appears sporadic when introduced to production data. It works fine with smaller datasets. After ...

What are the recommended methods for injecting db and logger into routes in an Express application?

When working with Express and needing to pass single instance references like a logger or database to routes, I have come across three options. The first option is to attach it to the request object through a middleware: app.use(function(req,res,next){ re ...

transferring double quotation marks and square brackets through a parameter

I have an angularjs (1.x) scope set up as follows: $scope.report = { resource: '/public/emplyACH', params: { "employeeId": [78] } }; When I try to access it using console.log (console.log(scope.parms)) I see the output as ...

Change the state within the click event handler

One issue I'm facing is dealing with 2 submit buttons in my react form. To differentiate between the two buttons, I need to extract the `id` of the one that was clicked using the `onClick` function. Currently, when trying to set the state with this ` ...

Unlock the power to toggle dynamic elements with jQuery

I'm currently using jQuery to enable and disable input elements, but I'm having trouble doing so for similar elements with the same class. Here is the HTML code: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">< ...

Deleting JSON files using Discord and Node.js by requiring them from a specific folder

Currently, I am in the process of developing a Discord bot using Node.js. One of the functions within the bot utilizes JSON files to store information about specific entities. I aim to create a command in Discord that, when called with a specific name asso ...

Prevent scale animation for a specific section of the icon using CSS

I need help in preventing the scale animation of the :before part of the icon class from occurring when the button is hovered. The current behavior is causing the arrow to look distorted on hover... Link to the code on Codepen HTML <div class="se ...

Tips for emphasizing matches within a string using JSX

I've implemented a custom autocomplete feature that suggests options based on user input. I want to highlight the characters in the suggestions list that match the input value. For example, if the suggestion list includes "alligator", "lima", and "li ...

Capturing information within a jQuery function by accessing data from another function

Can data be collected from another function while the function is running? // Custom Function function getData(){ var name = 'tom'; return name } // Main Target Area $('.myDiv').click(function(){ // I need to retrieve dat ...

ReactJS is giving me an error message: "Uncaught TypeError: Unable to access property 'ownerDocument' since it is null."

Trying to create a bar chart using D3 and render it with React's render method. Below is my index.js file: import React from 'react'; import ReactDOM from 'react-dom'; import './Styles/index.css'; import BarChart from &a ...

unable to log out firebase user

Currently, I am attempting to sign out the user who is already signed in within my Angular app. Here is my client service code: export class AuthClientService { public register(email: string, password: string): Observable<Object> { retu ...