In the function below, the existingFiles
object is returned empty.
If I print it at the end of the 'service.files.list' then I see the expected results.
What am I missing?
For sure there are older posts with the same/similar question but I possibly miss the correct keyword when searching for those.
function searchFiles(auth, fileName) {
"use strict";
let service = google.drive('v3');
let existingFiles = new Object();
service.files.list({
auth: auth,
q: "name contains '"+fileName+"' and trashed = false",
spaces: 'drive',
pageSize: 100,
fields: "nextPageToken, files(id, name)"
}, function(err, response) {
if (err) {
doNotify(hipchatChannelID, hipchatFromUser, 'The API returned an error: '+err,'red');
return;
}
let files = response.files;
if (files.length == 0) {
//console.log('No files found.');
} else {
for (let i = 0; i < files.length; i++) {
let file = files[i];
existingFiles = { 'fileName':file.name, 'fileID':file.id };
}
}
});
//console.log(JSON.stringify(existingFiles));
return existingFiles;
}