Search code examples
javascriptnode.jsgoogle-drive-apigoogle-api-js-client

Running do while loop results in fatal error


I am trying to use the google drive api to get list of files in a particular folder. Now when I try to run a do..while loop in order to fetch file list in small chunks, the app crashes with a fatal error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory  

Snippet

function listFiles(auth) {
    const drive = google.drive({ version: "v3", auth });
    let pageToken = null;

    do {
        drive.files.list({
            pageSize: 10,
            q: "'root' in parents and trashed=false",
            fields: "nextPageToken, files(id, name)",
            pageToken: pageToken,
        }, (err, res) => {
            if (err) return console.error(`The API returned an error: ${err}`);

            pageToken = res.data.nextPageToken;
            const files = res.data.files;

            if (files.length) {
                files.forEach((file) => {
                    console.log(`${file.name}  (${file.id})`);
                });
            } else {
                console.log("No files found!");
            }
        });
    }
    while(!pageToken);

AFAIK nextPageToken will be undefined if there are no more files.


Solution

  • I believe you’ve heard of the term "async" in JS. Apparently drive.files.list is a callback style async function. And this pageToken = res.data.nextPageToken assignment happens inside that callback, which means pageToken’s value doesn’t change instantly from null to something

    However your do...while logic happens synchronously. Thus while(!pageToken) basically equals while(true). That’s why you got the error, your program stuck in infinite loop.