2
votes

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.

1
Can you provide the other code that's calling that method? - MasNotsram
Sure! I'm developing a lib to make it easier to work with GDrive files. It's in a early stage here: 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
Yes me too. :) That helped me create the insert, update and find functionality but with the deleting I'm stuck. - arpo
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

1 Answers

3
votes

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.