9

The JavaScript example to list files of Google Drive uses gapi.client.drive.files.list(). When trying to use this method I get the error "Cannot read property 'files' of undefined".

The issue and a workaround are already described under Google Drive API javascript

Is the documentation under https://developers.google.com/drive/v2/reference/files/list#try-it incorrect? Or is there a way to use the API as described.

tehhowch
  • 9,221
  • 4
  • 23
  • 40
user1495551
  • 91
  • 1
  • 1
  • 2
  • Duplicate of http://stackoverflow.com/questions/11315962/google-drive-api-javascript – Steve Bazyl Jan 10 '13 at 19:18
  • @SteveBazyl: This is not a duplicate; this asks if there is a circumstance for which the example in the documentation is correct (the alternative being that the example is incorrect). The other question asks how to fix the error. – icktoofay May 11 '13 at 05:56
  • This will fix it: http://stackoverflow.com/questions/15589794/call-gapi-client-load-before-all-my-executions-correct – Aspen Jul 17 '15 at 08:41

2 Answers2

18

The JavaScript example is correct, but you have to make sure that you only use gapi.client.drive.files (and the other Drive-specific resources) when the Drive library is loaded, i.e. after:

gapi.client.load('drive', 'v2', callback);
Claudio Cherubino
  • 14,728
  • 1
  • 33
  • 40
7

If it works fine after writing gapi.client.load('drive', 'v2', callback), then all good. In my case it does not work so I wrote below code.

gapi.load('client', function () {
                gapi.client.load('drive', 'v2', function () {
                    var file = gapi.client.drive.files.get({ 'fileId': fileId });
                    file.execute(function (resp) {
                        //Write you code with resp variable
                    });

                });
            });