'how to upload files on google drive in a specified folder?

I am working on a youtube clone project and I have to upload files on the google drive. here is the code

//Save file to google
const saveFile = ({ filePath, filename, mimeType }) => {
  const promise = new Promise(async (resolve, reject) => {
    try {
      const drive = await getDrive();
      const bucket = await getBucket("BuckName"); //yaha phele BuckName se phele Youtube tha
      const file = await drive.files.create({
        resource: {
          name: filename,
          parents: [bucket.id],
        },
        media: {
          mimeType,
          body: fs.createReadStream(filePath),
        },
        fields: "id",
      });
      resolve(file.data);
    } catch (err) {
      console.log("this error is from savefile"+" "+err);
      reject(err);
    }
  });
  return promise;
};

/*
Preconditions: 
  A drive folder named "bucketName" must exist
  Note: I created a folder on Google Drive client and shared it with the service account email
Postconditions:
  Returns the first folder with name "buckName".
*/
const getBucket = (buckName) => {
  const promise = new Promise(async (resolve, reject) => {
    try {
      const drive = await getDrive();
      const folder = await drive.files.list({
        pageSize: 1,
        q: `mimeType='application/vnd.google-apps.folder' and name='${buckName}'`,
        fields: "files(id, name)",
      });
      resolve(folder.data.files[0]);
    } catch (err) {
      reject(err);
    }
  });
  return promise;
};

But I am getting error of Id is not defined I also create a folder named BucketName on the google drive and shared it to the service as precondition required but still getting error please help me with this am new in this field first time working this google API here is the error

TypeError: Cannot read property 'id' of undefined
    at E:\intern data\youtube project\git-lolly\Lolly\server\services\fileStorage.js:    at E:\intern data\youtube project\git-lolly\Lolly\server\services\fileStorage.js:
64:28

please have a look on precondition and post condition and here are the code or drive and getbucket

const getBucket = (buckName) => {
  const promise = new Promise(async (resolve, reject) => {
    try {
      const drive = await getDrive();
      const folder = await drive.files.list({
        pageSize: 1,
        q: `mimeType='application/vnd.google-apps.folder' and name='${buckName}'`,
        fields: "files(id, name)",
      });
      resolve(folder.data.files[0]);
    } catch (err) {
      reject(err);
    }
  });
  return promise;
};

const getDrive = () => {
  const promise = new Promise(async (resolve, reject) => {
    try {
      const auth = await google.auth.getClient({
        credentials: GOOGLE_DRIVE_CREDENTIALS,
        scopes: "https://www.googleapis.com/auth/drive",
      });
      resolve(
        google.drive({
          version: "v3",
          auth,
        })
      );
    } catch (err) {
      console.log("this error is from the getdrive" + " "+ err);
      reject(err);
    }
  });
  return promise;
};


Solution 1:[1]

Answer from the original poster (on the comments):

Here I have to return buckname in the getBuck function and also have to make a folder named buckname in the google drive.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1