使用pinoyyid的ngDrive模块获取数据返回空数组,求解决方案
Let's walk through the key issues in your code and fix them step by step:
1. You’re Handling Async API Calls Synchronously
The DriveService.files.list() method returns a Promise, not immediate response data. Your current code tries to assign .data directly, which won’t work because the API call hasn’t completed yet. Here’s how to adjust your controller:
var MainCtrl = (function () { function MainCtrl(DriveService) { var vm = this; // Use Promise .then() to handle async results DriveService.files.list({ q: 'trashed = false', maxResults: 10, fields: 'items/title' }, true) .then(function(response) { // Access the actual files array from the response vm.filesArray = response.data.items; }) .catch(function(error) { console.error('Failed to fetch files:', error); }); } MainCtrl.$inject = ['DriveService']; return MainCtrl; })();
2. Your Permission Scope Might Be Too Restrictive
You’re using the drive.file scope, which only grants access to files created or opened by your app. If you don’t have any such files in your Drive, you’ll get an empty array. For read access to all non-trashed files, switch to a broader (but still secure) scope:
OauthServiceProvider.setScopes('https://www.googleapis.com/auth/drive.readonly');
(Stick to the minimal scope you need for security — drive.readonly is safer than full drive access if you only need to list files.)
3. Ensure OAuth Authorization Is Completed
ngDrive requires a finished OAuth flow before it can call Drive APIs. Add an authorization flow to confirm the user has granted access:
First, add an authorization button to your HTML:
<button ng-click="vm.handleAuth()">Authorize Drive Access</button>
Then update your controller to handle the authorization flow:
var MainCtrl = (function () { function MainCtrl(DriveService, OauthService) { var vm = this; vm.handleAuth = function() { OauthService.checkAuth() .then(function() { console.log('Authorization successful'); loadFiles(); }) .catch(function() { // Trigger the authorization popup if not already authorized OauthService.auth(); }); }; function loadFiles() { DriveService.files.list({ q: 'trashed = false', maxResults: 10, fields: 'items/title' }, true) .then(function(response) { vm.filesArray = response.data.items; }) .catch(function(error) { console.error('Failed to fetch files:', error); }); } // Check authorization status when the page loads OauthService.checkAuth().then(loadFiles); } MainCtrl.$inject = ['DriveService', 'OauthService']; return MainCtrl; })();
4. Verify Your Fields Parameter
Your fields: 'items/title' is correct for Drive API v2, but make sure you’re extracting the items array from the response (as shown in the code above) — response.data is a wrapper object, not the files list directly.
5. Check the Browser Console for Errors
Open your browser’s DevTools (F12) and go to the Console tab. Look for authorization errors, invalid client ID warnings, or API request failures — these will give you direct clues if something’s wrong with your setup.
After making these changes, you should start seeing your Drive files populate in the list.
内容的提问来源于stack exchange,提问作者Sagar Chaudhary

