title: Problems with contentDetails, duration and statistics from the Youtube API v3
body: <p>I'm trying to retrieve the values for <code>duration</code> and <code>viewCount</code> for each video, both under videos:list api. I created a separate function that will return each video duration and statistics. The only thing that works is the playlist of videos that were retrieved and <code>videoId</code> within the <code>getPlaylist(...)</code> function. I logged to the console and this is what I'm getting:</p>
  
  <pre><code>console.log("id: " + videoId + " duration: " + vidDuration + " viewCount: " + viewCount); // return value in console
  </code></pre>
  
  <p><code>id: xxx duration: undefined viewCount: undefined</code> </p>
  
  <p>I never got duration to work, but I noticed that the api with <code>part: statistics</code> will retrieve the value of the total sum of view counts from channels where commented out. But it's not what I'm looking for. I want it to work with each video using: </p>
  
  <p><code>GET https://www.googleapis.com/youtube/v3/videos</code></p>
  
  <p>But for some reason, I can't get it to work. I have the <code>videoId</code> property and as parameter, so playlistId can read it and retrieve said value for the video's duration and view count, but it still remains undefined, even when the <code>videoId</code> has been defined. Oh, and the alert dialog box isn't showing up either.</p>
  
  <p>What am I missing here?</p>
  
  <p><strong>Script:</strong></p>
  
  <pre><code>var channelName = 'ExampleChannel';
  var vidWidth = 500;
  var vidHeight = 400; 
  var vidResults = 15; /* # of videos to show at once - max 50 */
  var vidDuration = "";
  var viewCount = 0;
  var videoId = "";
  
  $(document).ready(function() {
      $.get( // get channel name and load data
          "https://www.googleapis.com/youtube/v3/channels",
          {
              part: 'contentDetails',
              forUsername: channelName,
              key: 'XXXXXXXXXX'
          },
  
          function(data)
          {
              $.each(data.items, 
                  function(i, item) {
                      console.log(item); // log all items to console
                      var playlistId = item.contentDetails.relatedPlaylists.uploads;
                      //var viewCount = console.log(item.statistics.viewCount);
                      getPlaylists(playlistId);
  
              })
          }         
      );
  
      // function that gets the playlists
      function getPlaylists(playlistId)
      {
          $.get(
              "https://www.googleapis.com/youtube/v3/playlistItems",
              {
                  part: 'snippet',
                  maxResults: vidResults,
                  playlistId: playlistId,
                  key: 'XXXXXXXXXX'
              },
  
              // print the results
              function(data)
              {
                  var output;
                  $.each(data.items, 
                      function(i, item) {
                          console.log(item);
                          var vidTitle = item.snippet.title; // video title
                          var vidDesc = item.snippet.description; // video description
                          var videoId = item.snippet.resourceId.videoId; // video id
  
                          // check if description is empty
                          if(vidDesc == null || vidDesc == "")
                          {
                              vidDesc = "No description was written."; // FIX: test msg to see where it still shows up
                              $('#desc').remove(); // remove video description
                          }
                          else vidDesc = item.snippet.description;
  
                          vidDuration = getVideoDuration(videoId);
                          viewCount = getViewCount(videoId);
                          console.log("id: " + videoId + " duration: " + vidDuration + " viewCount: " + viewCount); // return value in console
  
                          output = '&lt;li&gt;&lt;iframe height="' + vidHeight + '" width="' + vidWidth + '" src=\"//www.youtube.com/embed/' + videoId + '\"&gt;&lt;/iframe&gt;&lt;/li&gt;&lt;div id="title"&gt;' + vidTitle + '&lt;/div&gt;&lt;div id="desc"&gt;' + vidDesc + '&lt;/div&gt;&lt;div id="duration"&gt;Length: ' + vidDuration + '&lt;/div&gt;&lt;div id="stats"&gt;View Count: ' + viewCount + '&lt;/div&gt;';
  
                      // Append results to list tag
                      $('#results').append(output);
                  })
              }         
          );
      }
  
      // return video duration
      function getVideoDuration(videoId) 
      { 
          $.get(
              "https://www.googleapis.com/youtube/v3/videos",
              {
                  part: 'contentDetails',
                  id: videoId,
                  key: 'XXXXXXXXXX',
              },
  
              function(data)
              {
                  $.each(data.items,
                      function(i, item) {
                          //videoId = item.snippet.resourceId.videoId;
  
                          alert(item.contentDetails.duration); // video duration
                          //alert(videoId);
                      })    
              }
          );
      }
  
      // return video view count
      function getViewCount(videoId) 
      { 
          $.get(
              "https://www.googleapis.com/youtube/v3/videos",
              {
                  part: 'contentDetails, statistics',
                  id: videoId,
                  key: 'XXXXXXXXXX',
              },
  
              function(data)
              {
                  $.each(data.items,
                      function(i, item) {
                          //videoId = item.snippet.resourceId.videoId;
  
                          alert(item.statistics.viewCount); // view count
                          //alert(videoId);
                      })  
              }
          );
      } 
  });
  </code></pre>
  
  <p>Screenshot: (Update)</p>
  
  <p><a href="https://i.stack.imgur.com/qNwWJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qNwWJ.png" alt="enter image description here"></a></p>