Error while implementing Algolia Google Drive Integration

UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property ‘data’ of ‘undefined’ as it is undefined.

Getting the above error while implementing searching with google drive.

function indexDocument(client, documents, cursor) {
    // step 1: we get the metadata
    const document = documents[cursor];
    if (!document) return;

    getFile(client, document.id)
        .then(document => {
            console.log(document);
            // step 2: we only want to index Google documents
            if (document.mimeType === 'application/vnd.google-apps.document') {
                // step 3: we download the documents
                download(client, document.id, (err, { data }) => {
                    const chunks = [];
                    const records = [];
                    const text = data;
                    if (text.length > 5000) {
                        const chuncksText = getChunks(text);
                        chunks.concat(chuncksText);
                    } else {
                        chunks.push(text);
                    }
                    // step 4: we create the Algolia records
                    for (let j = 0; j < chunks.length; j++) {
                        records.push({
                            googleId: document.id,
                            name: document.name,
                            modifiedTime: document.modifiedTime,
                            content: chunks[j]
                        });
                    }
                    // step 5: we push them to Algolia and continue indexing the list of documents
                    if (records.length) {
                        index.addObjects(records, (err, res) => {
                            indexDocument(client, documents, cursor + 1);
                        });
                    } else {
                        indexDocument(client, documents, cursor + 1);
                    }
                });
            } else {
                indexDocument(client, documents, cursor + 1);
            }
        })
        .catch(e => e);
}

@yashsa welcome to the community!

I believe an error is being thrown and that is why the object it is trying to destructure is undefined.

In the snippet below, I’ve removed the default destructuring with just result, this way if that variable is undefined you won’t hit an error. Inside of the callback for download, I added a debug line just to show what the values are. Below that, I check if the err variable is truthy and if so, I print out the error and exit the function so we don’t continue since we hit an error. For assigning the text variable, I just changed this to result.data as we don’t have the destructuring anymore.

function indexDocument(client, documents, cursor) {
    // step 1: we get the metadata
    const document = documents[cursor];
    if (!document) return;

    getFile(client, document.id)
        .then(document => {
            console.log(document);
            // step 2: we only want to index Google documents
            if (document.mimeType === 'application/vnd.google-apps.document') {
                // step 3: we download the documents
                download(client, document.id, (err, result) => {
                    console.log('Download debug', document.id, 'Err:', err, '; Result:', result)
                    if (err) {
                      console.error('Unable to download document', document.id, err);
                      return;
                    }
                    const chunks = [];
                    const records = [];
                    const text = result.data;
                    if (text.length > 5000) {
                        const chuncksText = getChunks(text);
                        chunks.concat(chuncksText);
                    } else {
                        chunks.push(text);
                    }
                    // step 4: we create the Algolia records
                    for (let j = 0; j < chunks.length; j++) {
                        records.push({
                            googleId: document.id,
                            name: document.name,
                            modifiedTime: document.modifiedTime,
                            content: chunks[j]
                        });
                    }
                    // step 5: we push them to Algolia and continue indexing the list of documents
                    if (records.length) {
                        index.addObjects(records, (err, res) => {
                            indexDocument(client, documents, cursor + 1);
                        });
                    } else {
                        indexDocument(client, documents, cursor + 1);
                    }
                });
            } else {
                indexDocument(client, documents, cursor + 1);
            }
        })
        .catch(e => e);
}

What is the error that is being thrown? Happy to help further, let us know! Thanks!