Delete file in Google Drive with Javascript - javascript

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.

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.

Related

loadJSON not Defined - Chrome Extension Error

I'm incredibly new to HTML/Javascript/API's and im trying to make my first chrome extension using coinmarketcaps' public API.
API: https://api.coinmarketcap.com/v2/ticker/?limit=12&sort=rank&convert=ETH
My Code:
console.log("Running");
function setup() {
loadJSON('https://api.coinmarketcap.com/v2/ticker/?
limit=12&sort=rank&convert=ETH', gotData, 'jsonp');
}
setup();
function gotData(data){
console.log(data);
list = data;
}
gotData();
Essentially what I want to do is load in the data and then print the data to my console to make sure that it is in fact loaded. The error that I'm getting when i check the background.js console after uploading the extension is 'loadJSON is not defined'
I don't know if im missing a package or am unable to use such a command but after looking online for a while I can't seem to figure it out. Another thought that I had was maybe it can't be used in a background.js file. Also, I am using Atom software for this project. Let me know if you need any more information, any help is appreciated.

IONIC - can't access externalDataDirectory on $ionicView.enter

I'm trying to read a directory to check how many files exists
When I open the app I get this error
ionic.bundle.js:26799 TypeError: Cannot read property
'externalDataDirectory' of undefined
But when I go to another view, and came back, this works fine.
I try to call the same function on
$ionicPlatform.ready and works, but this way is executable only once, I need to call every time I enter on this page.
Anyone had this error before?
I'm using ionic v1 and $cordovaFile extension
Thank you
Wrap your code with $ionicPlatform.ready:
$scope.$on('$ionicView.enter', function() {
$ionicPlatform.ready(function() {
// cordova file code
});
});

I can't use the browser object from the settings page of my Firefox add-on

I'm making a Firefox Add-On that needs to keep track of a list of words. From the main javascript file I can read and modify a stored Array under the name "list", but when I try to access it from the settings page I get an error:
ReferenceError: browser is not defined
Here is the function from which I try to access the local storage (this is in the .js file that is used in my .html settings page).
function EmptyList() {
if (confirm("Empty list?")){
browser.storage.local.set({
list : []
});
}
}
I don't understand what am I doing wrong. I'm not doing anything different from what they do in the mozilla tutorial.
Apparently it has something to do with the cache.
Disable and reenable the add-on (or remove it and add it again) and it works. Go figure.

Using Parse's JavaScript framework with Google Apps Script

I would like to use the Parse framework directly in Google Apps Script and copied the following source code from Parse.com directly into my project.
However, it seems that there are some adjustments required to get this to work correctly. e.g. when running the following sample code...
function upload()
{
Parse.initialize("wCxiu8kyyyyyyyyyyyyyyyyy", "bTxxxxx8bxxxxxxxxx");
var TestObject = Parse.Object.extend("TestObjectJSSSSS");
var testObject = new TestObject();
testObject.save({foo: "bar"}, {
success: function(object) {
alert("yay! it worked");
}
});
}
… I get the error message TypeError: Cannot call method "getItem" of undefined.
which seems to relate to localStorage. I believe I should replace localStorage with a similar storage type available in Google Apps Script. Would that be possible and how would I need to adjust the Parse code?
What other adjustments would I need to get to the Parse framework to work in my Google Apps Script project?
I would suggest that using Parse REST API would be a far simpler solution and it is meant for such cases.

Dynamically load JavaScript with JavaScript

After over an hour of trying to get it to work I'm thinking it's because of cross domain policies, but I really thought this would work. I can't find a lot of info on it either. But, here is my issue. I have a site called http://mysite.com and then I include a 3rd party script (what im writing) and its at http://supercoolsite.com/api/script.js and this script needs to dynamically load the google maps api at: http://maps.google.com/maps/api/js?sensor=false before it runs. Well, i figured this code would work:
function loadScript(filename,callback){
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
fileref.onload = callback();
if (typeof fileref!="undefined"){
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
loadScript('http://maps.google.com/maps/api/js?sensor=false',function(){
console.log('done loading');
init();
});
But my response in my console is:
api.js:408 done loading
api.js:115 test
api.js:310 Uncaught ReferenceError: google is not defined
the "test" is at the top of the init(). So, it's loading the script, but it's not executing it, it seems. So, any ideas? If it is a cross site scripting issue my only method of fixing this i could think of is having a PHP script on our end that basically just sets the header to a text/javascript header and then echo file_get_contents() into a googlemaps.php file we host. About to try this as we speak, but, if possible, a way to do it with pure JS would be awesome.
P.S. I also tried adding jQuery, then doing getScript(), and it still didnt work
-- UPDATE --
See this fiddle:
http://jsfiddle.net/ycMCa/2/
You'll see that you get the error in your console:
Uncaught TypeError: undefined is not a function
Despite that the google variable is global.
You just have a minor error:
fileref.onload = callback();
This will call callback immediately and assign its return value to fileref.onload.
It should be
fileref.onload = callback;
You also should add the handler before you set the source (just in case).
DEMO

Categories

Resources