Reputation: 153
I'm trying to make javascript code to list the names of all the files in the root directory of Google Drive. FYI: I'm new to javascript.
For the first function, I copied code from https://developers.google.com/drive/v2/reference/children/list .
The next two functions are my own code. My results is just a bunch of "[object Object],[object Object],[object Object],[object Object]". This output matches the number of files I have in the root folder of my Google Drive. However, I want the file names or id's, but I can't seem to get anything besides "[object Object]".
I tried changing the "resp.items" to "resp.kind" or "resp.selflink" but then I just get "undefined". Anyone know how to get any specific information on the files? In case it matters, scope is "https://www.googleapis.com/auth/drive".
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);
}
});
}
var initialRequest = gapi.client.drive.children.list({
'folderId' : folderId
});
retrievePageOfChildren(initialRequest, []);
}
function printToOutdiv (result){document.getElementById("outdiv").innerHTML=result;}
function GetFilesButton (){
gapi.client.load('drive', 'v2', function() {retrieveAllFilesInFolder('root',printToOutdiv);} );
}
Upvotes: 1
Views: 1977
Reputation: 15014
The result you are getting is an array of objects that you can access by index, as in result[0]
, result[1]
, and so on.
Once you get a reference to one of those objects, you can access its properties (e.g. the id) as in result[0].id
.
Upvotes: 1