When I run this, I get an error: Uncaught TypeError: Cannot read property 'files' of undefined on the line with gapi.client.drive.files.list. What am I doing wrong?
I’ve encountered a similar issue before, and it sounds like the Google Drive API might not be loading correctly. Here’s what worked for me:
In your fetchFileList function, try adding a check to ensure the Drive API is fully loaded before making the request:
function fetchFileList() {
gapi.client.load('drive', 'v3', function() {
if (gapi.client.drive) {
sendRequest();
} else {
console.error('Drive API failed to load');
}
});
}
Also, make sure you’re using the correct API version. I noticed you’re using ‘v2’, but ‘v3’ is the current version. Update your gapi.client.load call to use ‘v3’ instead.
Lastly, double-check that you’ve enabled the Drive API in your Google Cloud Console project. Sometimes, this step is easy to overlook.
If you’re still having trouble after these changes, try adding some console.log statements throughout your code to pinpoint where exactly the error is occurring. This helped me immensely when debugging my own implementation.