jim
jim

Reputation: 73

Create folder returns undefined when trying to find file id

I am using Google drive API to create a folder and I want to return the id of this folder after creating it so I can make a folder tree. I don't understand why I am unable to do this, even using promises I consistently get the result of undefined for the id of the folder. This code returns undefined for the id of the folder. I don't want to use the list method to search the drive after creating the folder because it's slow. Any ideas?

const fs = require('fs');
const readline = require('readline');
const {google, run_v1} = require('googleapis');

const SCOPES = ['https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = 'token.json';


const CLIENT_NAME = 'Bowie, David';

const ROOT_FOLDER_LOCATION = '11ljl1lk1lk';


function getCreds(){
  return new Promise((resolve, reject) => {
    fs.readFile('credentials.json', (err, content) => {
      if (err) {
        reject('error loading client secret');
      } else{
        resolve(JSON.parse(content));
      }
    })
  })
}

function authorize(creds){
  const {client_secret, client_id, redirect_uris} = creds.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);
  return new Promise((resolve, reject) => {
    
    fs.readFile(TOKEN_PATH, (err, token) => {
      if (err) {
        reject('this failed authorization');
      } else {
        oAuth2Client.setCredentials(JSON.parse(token));
        resolve(oAuth2Client);
      }
    })
  })
}


function makeRoot(auth) {
  const drive = google.drive({version: 'v3', auth});
  var fileMetadata = {
    'name': CLIENT_NAME,
    'mimeType': 'application/vnd.google-apps.folder',
    'parents': [ROOT_FOLDER_LOCATION]
  };
  return new Promise((resolve, reject) => {
    drive.files.create({
      resource: fileMetadata,
      fields: 'id'
    }, function(err, file) {
      if (err) {
        reject(err);
      } else {
        resolve('Folder ID:', file.id);
      }
    })
  })

}


async function doWork() {
  
  try {
    const response = await getCreds();
    const oAuth2Client = await authorize(response);
    const rootFolder = await makeRoot(oAuth2Client);
    console.log(rootFolder);
  } catch (err) {
    console.log(err)
  }
  
}

doWork();

The only output I get is "undefined"...

Upvotes: 1

Views: 310

Answers (1)

Tanaike
Tanaike

Reputation: 201613

Modification points:

  • When you want to retrieve the folder ID from the created new folder using googleapis for Node.js, in the case of your script, you can retrieve it using file.data.id. Ref1, Ref2
  • But, in your script, when resolve('Folder ID:', file.id); is run, rootFolder of const rootFolder = await makeRoot(oAuth2Client); is Folder ID:.

When you want to retrieve the value of Folder ID:###folderId###, please modify as follows.

From:

resolve('Folder ID:', file.id);

To:

resolve("Folder ID:" + file.data.id);
  • If you want to separate the values of Folder ID: and ###folderId###, you can also use the following modification. In this case, you can retrieve the folder ID like const [text, rootFolder] = await makeRoot(oAuth2Client);.

      resolve(["Folder ID:", file.data.id]);
    

Reference:

Upvotes: 1

Related Questions