2

I'm trying to delete a file using the GDriva API for JavaScript. This page is quit straight forward it seems, but it doesn't work. https://developers.google.com/drive/v2/reference/files/delete

Looks like it should be easy to do

function deleteFile(fileId) {
  var request = gapi.client.drive.files.delete({
    'fileId': fileId
  });
  request.execute(function(resp) { });
}

But I get "Uncaught TypeError: Cannot read property 'files' of undefined"

Does anyone know what's wrong? I have all permissions right. I can create and update a file but not remove it.

UPDATE! Found this: Deleting a Google Drive file using JS client. There's seems to be a bug in the API. There's a solution which delete the document so that you can't find it with the API, using list, but the document will remain in your Google Drive and will be corrupted. You can view it but not remove or open it.

Community
  • 1
  • 1
arpo
  • 1,831
  • 2
  • 27
  • 48
  • Can you provide the other code that's calling that method? – MasNotsram May 13 '13 at 12:53
  • Also, read this thread: http://stackoverflow.com/questions/11315962/google-drive-api-javascript?rq=1 – MasNotsram May 13 '13 at 12:53
  • Sure! I'm developing a lib to make it easier to work with GDrive files. It's in a early stage here: http://www.verodella.se/snipply/ The error is in the console. The deleting takes place in the bottom at the file: VRD.gd.js, see the source. – arpo May 13 '13 at 12:57
  • Yes me too. :) That helped me create the insert, update and find functionality but with the deleting I'm stuck. – arpo May 13 '13 at 12:59
  • In the insert, update etc methods you're using the gapi.client.request syntax. I would suspect the API is either wrong or ahead of itself (the gapi.client.drive syntax may come in a future release). I would just use the commented out gapi.client.request syntax in the delete method. – MasNotsram May 13 '13 at 13:09
  • The problem with the commented out syntax is that it doesn't work. :( Found this http://stackoverflow.com/questions/15532468/deleting-a-google-drive-file-using-js-client?rq=1 that might work. But think it would be nice to get a more proper solution. – arpo May 13 '13 at 13:14

1 Answers1

3

Sounds like you didn't load the drive client library. Your error message says that gapi.client.drive is undefined. You should have a line like:

gapi.client.load('drive', 'v2', function() { /* Loaded */ });

which will load the drive API and define gapi.client.drive. Make sure you either call delete in the callback, or otherwise ensure that drive is loaded before trying to delete a file.

Or, as @MasNotsram mentioned, you could just use the gapi.client.request syntax for calling delete.

Ron Romero
  • 9,211
  • 8
  • 43
  • 64