Search code examples
node.jsfirebasegoogle-cloud-firestoregoogle-cloud-tasks

CloudTasksClient causing error when instantiated in Firebase Function's onDocumentCreated


I have whittled my firebase functions index.ts down to the code below and I am getting errors in the Google Cloud logs when a document is created. The error is related to new CloudTasksClient(). If I remove that and just log a simple string then I am seeing the record and log when a document is created an no errors. @google-cloud/tasks is updated. even if I just try to log logger.log('CloudTasksClient', CloudTasksClient); without instantiating it, I get these errors as well which seems to point to an import issue but I cannot for the life of me figure out what it is. Any ideas?

import { initializeApp } from 'firebase-admin/app';
import * as logger from 'firebase-functions/logger';
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
import { CloudTasksClient } from '@google-cloud/tasks';

initializeApp();

export const onStartSession = onDocumentCreated(
    'userSessionInfo/{userId}/history/{sessionId}',
    async () => {
        const project: string = /* removed */;
        const location: string = 'us-central1';
        const queue: string = 'delay';
        const tasksClient = new CloudTasksClient();
        const queuePath = tasksClient.queuePath(project, location, queue);
        logger.log('queuePath', queuePath);
    },
);

These are the errors. It seems like the class is undefined which is throwing the second error and then the first one is from Firebase being unable to create a snapshot when the document is created because of the undefined error?

Error: Failed to decode protobuf and create a snapshot.
    at entryFromArgs (/workspace/node_modules/firebase-functions/lib/logger/index.js:130:19)
    at Object.error (/workspace/node_modules/firebase-functions/lib/logger/index.js:116:11)
    at createSnapshotFromProtobuf (/workspace/node_modules/firebase-functions/lib/common/providers/firestore.js:61:16)
    at createSnapshot (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:119:59)
    at makeFirestoreEvent (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:156:15)
    at func (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:222:32)
    at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:113:25
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of undefined (reading 'cloud')
    at Function.decode (/workspace/node_modules/firebase-functions/protos/compiledFirestore.js:1529:130)
    at createSnapshotFromProtobuf (/workspace/node_modules/firebase-functions/lib/common/providers/firestore.js:57:52)
    at createSnapshot (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:119:59)
    at makeFirestoreEvent (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:156:15)
    at func (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:222:32)

Solution

  • There is an issue which is actively investigated: https://github.com/googleapis/google-cloud-node/issues/5000

    https://github.com/sofisl seems to be looking into it right now.

    A quick fix would be a down-grade to @google-cloud/tasks v4.0.1.