0

The following code returns an error in the chrome console:

Uncaught TypeError: Cannot read property 'children' of undefined 

Initial error appeared 5 lines from last script close tag, and at a later point (currently), appears in line 38 of gapi.loaded_0

Combining these two code chunks https://developers.google.com/drive/v2/reference/children/list#examples and https://developers.google.com/drive/web/quickstart/quickstart-js#step_2_set_up_the_sample to get below to list children of a folder

<html>
  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script type="text/javascript">
      var CLIENT_ID = 'semi-secret-clientIdString.apps.googleusercontent.com';
      var SCOPES = 'https://www.googleapis.com/auth/drive';
      var thisFolderId = 'folderIdString';  

      /**
       * Called when the client library is loaded to start the auth flow.
       */
      function handleClientLoad() {
        window.setTimeout(checkAuth, 1);
      }

      /**
       * Check if the current user has authorized the application.
       */
      function checkAuth() {
        gapi.auth.authorize(
            {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
            handleAuthResult);
      }

      /**
       * Called when authorization server replies.
       *
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        var authButton = document.getElementById('authorizeButton');
        var lister = document.getElementById('Lister');
        authButton.style.display = 'none';
        lister.style.display = 'none';
        if (authResult && !authResult.error) {
          // Access token has been successfully retrieved, requests can be sent to the API.
          lister.style.display = 'block';
          lister.onchange = retrieveAllFilesInFolder(thisFolderId);
        } else {
          // No access token could be retrieved, show the button to start the authorization flow.
          authButton.style.display = 'block';
          authButton.onclick = function() {
              gapi.auth.authorize(
                  {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
                  handleAuthResult);
          };
        }
      }



///// Start retrieval function


function retrieveAllFilesInFolder(folderId,callback) {

var retrievePageOfChildren = function(request, result) {
    request.execute(function(resp) {
      result = result.concat(resp.items);
      var nextPageToken = resp.nextPageToken;
      if (nextPageToken) {
        request = gapi.client.drive.children.list({
          'folderId' : folderId,
          'pageToken': nextPageToken
        });
        retrievePageOfChildren(request, result);
      } else {
        callback(result);
      }
    });
  }
  /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<error on the next line>>>>>>>>>>>>>>>>>>>>>*/
  var initialRequest = gapi.client.drive.children.list({
      'folderId' : folderId
    });
  retrievePageOfChildren(initialRequest, []);
}
/////
    </script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </head>
  <body>
<input id="Lister" type="button" value="clickme" style="display: none" />
<input type="button" id="authorizeButton" style="display: none" value="Authorize" />

  </body>
</html>
user3645994
  • 409
  • 6
  • 13
  • 1
    _get chilrend of undefined_ - where exactly is this error? – Krease Jan 08 '15 at 07:35
  • 5 lines up from script close tag – user3645994 Jan 08 '15 at 08:15
  • `gapi.client.drive` is `undefined` (aka, this property doesn't exist), so you can't access `children` from there. There are several related questions [here](http://stackoverflow.com/questions/11315962/google-drive-api-javascript) and [here](http://stackoverflow.com/questions/14262884/google-drive-javascript-api-gapi-client-drive-files-undefined) from other people having the same problem - I suspect the answers there will help you. – Krease Jan 08 '15 at 08:29
  • Thanks! that helped, although now I run into an 'exceeded daily quota usage', putting down console.log(result) shows continuous objects and they are identical (https://www.googleapis.com/drive/v2/files/folderId/children/folderId). Do you have an idea of what in this code could be causing that? – user3645994 Jan 08 '15 at 08:55

0 Answers0