I’m working on a JavaScript project where I need to display all file names from my Google Drive root folder. I’m pretty new to working with APIs in JavaScript so I might be missing something basic.
I found some example code online and modified it, but when I run it, I only get “[object Object],[object Object]” repeated for each file instead of the actual file names. The count matches my files so the API call is working, but I can’t figure out how to extract the file names or IDs from the response.
I’ve tried accessing different properties like response.kind and response.selflink but those just return undefined. Here’s my current code:
function getAllDriveFiles(parentId, callbackFn) {
var fetchChildrenPage = function(apiRequest, resultArray) {
apiRequest.execute(function(apiResponse) {
resultArray = resultArray.concat(apiResponse.items);
var nextToken = apiResponse.nextPageToken;
if (nextToken) {
apiRequest = gapi.client.drive.children.list({
'folderId': parentId,
'pageToken': nextToken
});
fetchChildrenPage(apiRequest, resultArray);
} else {
callbackFn(resultArray);
}
});
}
var firstRequest = gapi.client.drive.children.list({
'folderId': parentId
});
fetchChildrenPage(firstRequest, []);
}
function displayResults(data) {
document.getElementById("output").innerHTML = data;
}
function startFileRetrieval() {
gapi.client.load('drive', 'v2', function() {
getAllDriveFiles('root', displayResults);
});
}
What am I doing wrong? How can I get the actual file names to display instead of object references?