Drive API - Попытка использовать идентификатор искомого элемента, но переменная не обновляет свое значение ... Но получает только «неопределенное» - PullRequest
0 голосов
/ 12 сентября 2018

Я пытаюсь найти файл на командном диске, к которому у моей учетной записи службы есть доступ, а затем сохранить идентификатор файла, который пришел к переменной, и использовать эту переменную позже.

Проблема здесь в том, что переменная никогда не обновляется и возвращает только «неопределенное».

const {google} = require('googleapis');
const privatekey = require("./service.json");

// Extracting SA Info
const jwtClient = new google.auth.JWT(
  privatekey.client_email,
  null,
  privatekey.private_key,
  ['https://www.googleapis.com/auth/drive']);

// Authenticating SA
jwtClient.authorize(function (err, tokens) {
    if (err) {
        console.log(err);
        return;
    } else {
        console.log("\nSuccessfully connected!");
    }
});

// Defining Google Drive API
const drive = google.drive('v3');

var testValue; //Trying to use this to get the ID of searched file

var fileName = "Red";
var teamDriveID = "x0s0s00s0s0s0s0";
var pageSize = 1;
var fields = "files(id, name, parents, size)";
var orderBy = "quotaBytesUsed desc";

// List Team Drive Files function
function listFiles(fileName, teamDriveID, pageSize, fields, orderBy) {
    drive.files.list({
        auth: jwtClient,
        q: `name contains '${fileName}' and mimeType contains 'video/'`, 
        pageSize: pageSize,
        supportsTeamDrives: true,
        teamDriveId: teamDriveID,
        includeTeamDriveItems: true,
        fields: fields,
        corpora: 'teamDrive',
        // orderBy: orderBy
    },
    function (err, response) {
    if (err) {
        console.log('\nThe API returned an error: ' + err);
        return;
    } 
    const files = response.data.files;
    if (files.length == 0) {
        console.log("No files found...");
    } else {
            console.log('Found Files: ')
            for (const file of files) {
                console.log(`${file.name}` + " - " + "("+`${file.id}`+")" + " - " + `${file.parents}` + " -- " + `${file.size}`);
                testValue == file.id;
                return testValue;
            }
        }
    });
};

listFiles(fileName, teamDriveID, pageSize, fields, orderBy)

console.log(testValue) //printing testValue to check if its updated with item ID
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...