Google-api-nodejs-client: Drive API V3 - Files: list "q" parameter doesn't return files

Created on 10 Feb 2020  路  7Comments  路  Source: googleapis/google-api-nodejs-client

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS: Linux
  • Node.js version: 10.16.3
  • npm version: 6.9.0
  • googleapis version: ^39.2.0

Steps to reproduce

The "q" parameter is not returning the files in a folder when using the next query "'folder-id' in parents", although when using axios or the Try this API works as intended.

  1. Use the Try this API to check how the "q" parameter should work.
  2. Use this method to call the Drive API and verify it's not returning any value:
const queryFolders = (drive) => {
  // Return the Promise result after completing its task
  return new Promise((resolve, reject) => {
      // Call Files: list endpoint
      return drive.files.list({
        "q": "'folder-id' in parents",
        "fields": 'kind, files(id, name)'
      },(err, results) => err ? reject(err) : resolve(results))
    } 
  );
}

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

drive needs more info p2 bug

All 7 comments

@elgatovielma :wave: thanks for the bug report 馃憤 sounds like you have a workaround for the time being?

@bcoe, Thank you for answering! Yes, as a workaround I used axios as in the following code:

const queryFoldersWithAxios = async () => {
  const dummyData = [{id: "folder-id-1"}, {id: "folder-id-2"}];
  const params = {
    q: dummyData.map(folder => `'${folder.id}' in parents`).join(' or ')
  };
  const headers = {
    "Authorization": "Bearer <YOUR_ACCESS_TOKEN>", 
    "Content-Type": "application/json"
  }
  return await axios.get("https://www.googleapis.com/drive/v3/files", {params, headers})
}

The access token I got it from OAuth 2.0 Playground for testing purposes.

FWIW: I tried the workaround above and I was getting the same issue I had previously.

Preface: https://stackoverflow.com/questions/60131503/is-it-possible-to-query-for-multiple-folders-parents-using-googles-drive-api/60153138#comment106521729_60153138

As described in the S.O. post, my issue was drive.google.list not returning files found within any of my folders I had in my query, using the same query elgatovielma is using: '<folder-id-1>' in parents or '<folder-id-2>' in parents or.... Instead of receiving a list of all files in either of the specified folders, I either was returned an empty array of files, or an array of files populated with one item, a single folder.

However, as of tonight when I had the chance to glance at my S.O. post and this issue that elgato kindly created, thank you for that, I noticed more inconsistent and unusual behavior that drive.google.list was returning.


Folder contents

  • Folder A: Only images
  • Folder B: Only images
  • Folder C: 1 folder, and the rest images
  • Folder D: 1 folder, and the rest images
  • Folder E: 7 folders, nothing else

Query 1: 'A' in parents or 'B' in parents

The following query of two folders, both only containing images returned** a list that contained only one item, the first image in folder 'A'. The result was not intended, expecting all files found in 'A' or in 'B'. What was missing was the rest of the images found in 'A' and all the images found in 'B'.

Query 2: 'B' in parents or 'A' in parents

The following is identical to Query 1 except that the ordering of the query is swapped. The result is the same, returning the first image in folder 'A'. The result was not intended, expecting all files found in 'B' or in 'A'. What was missing was the rest of the images found in 'A' and all the images found in 'B'.

Query 3: 'A' in parents or 'C' in parents

The following is a query of two folders, one containing only images, and the other containing one folder and the rest as images. The result of files was one image and one folder. The result was not intended, expecting all files found in 'A' or in 'C'. What was missing was the rest of the images found in 'A' and all the images in 'C'.

Query 4: 'C' in parents or 'D' in parents

The following is a query of two folders, both containing only one folder and the rest of the files as images. The result of files was the two respective folders contained within each of the queried folders. The result was not intended, expecting all files found in 'C' or in 'D'. What was missing was all the images found in either 'C' or 'D'.

Query 5: 'A' in parents or 'E' in parents

The following is a query of two folders, one containing only images, and the other containing only 7 folders. The result of files was one image and 7 folders. The result was not intended, expecting all images found in 'A' and all folders in 'E'. What was missing was the rest of the images found in A.

Query 6: 'C' in parents or 'E' in parents

The following is a query of two folders, one containing one folder and the rest as images, and the other containing only 7 folders. The result of files was 8 folders. The result was not intended, expecting all images and the single folder in C and all folders in E. What was missing was all the images found in C.


The following results of the queries above is leading me on to believe that google.drive.list is behaving as such:

  • Both folders contain only images

    • result has one image

    • expected all images

  • One folder has only images, the other folder has a folder and images

    • result has one image and one folder

    • expected all images and one folder

  • Both folders has a folder and images

    • result has two folders and no other images

    • expected all images and two folders

  • One folder has only images, the other folder has only folders

    • result has one image and all folders

    • expected all images and all folders

  • One folder has a folder and images, the other has only folders

    • result has 8 folders

    • expected all images alongside the 8 folders

I really hope this helps in solving this issue. I have attached to this reply a horribly organized and _written_ list of my queries along with the actual folders I am querying in case the person attempting to solve this issue want to see for themselves the contents.
google-drive-list bug - Queries.txt

Should

"q": "'folder-id' in parents",

be:

"q": "'${folder-id}' in parents",

or:

"q": "'" + folder-id +"' in parents",

I use the latter, and it works

@mattcobb if I'm following correctly, the issue is simply that you need additional quotes around the folder-id?

folder-id is variable containing the parent ID, right? You want the value of folder-id to be expanded inside single quotes so q becomes, for example "'1muYqa15XxOEDIk4xXIZrOaOKZTWTc0bb' in parents". Currently, if I read that code correctly, it is literally the string "'folder-id' in parents" without the value of folder-id being used.

@elgatovielma I believe @mattcobb has correctly pointed out that the problem was simply that folder ID should be a variable, as follows:

const queryFolders = (drive) => {
  // Return the Promise result after completing its task
  return new Promise((resolve, reject) => {
      // Call Files: list endpoint
      return drive.files.list({
        "q": `'${folder.id}' in parents`,
        "fields": 'kind, files(id, name)'
      },(err, results) => err ? reject(err) : resolve(results))
    } 
  );
}

Just like in your axios example.

I'm closing this issue now as part of triage, please let me know if I've misunderstood 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joseparoli picture joseparoli  路  3Comments

suparnavg picture suparnavg  路  3Comments

JustinBeckwith picture JustinBeckwith  路  3Comments

rainabba picture rainabba  路  4Comments

streamnsight picture streamnsight  路  4Comments