I'm trying to get started with Cloud Functions for Firebase and trying to implement a simple onCall
handler. I have copied the default code from the documentation for v2 functions and reduced it to get the simplest possible code to rule out other issues. For reference here is a minimal index.js
:
const {onCall, HttpsError} = require("firebase-functions/v2/https");
exports.helloHttp = onCall((request) => {
return {
'data': request.data.name
}
});
and the associated package.json
:
{
"type":"module",
"dependencies": {
"firebase-functions": "^5.0.0"
}
}
When I try to run the code using the "Run Test" button in Google Cloud Functions Console I get the following error:
[9:08:51 PM] - TypeError: Cannot read properties of undefined (reading 'on')
at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:392:17
at new Promise (<anonymous>)
at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:391:16
at /workspace/node_modules/firebase-functions/lib/v2/trace.js:16:28
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)
I'm using node.js runtime version 20 and unless I'm missing something obvious, it feels like a bug in the imported onCall
function.
Does anyone have an idea of how to get onCall
to work in Cloud Functions? I could switch to using standard HTTP requests, but I want to use the authentication stuff associated with the Callables and it seems like this should be easy(?).
It's not possible to run functions from the Google Cloud console that use the firebase-functions module. The only correct use of that module absolutely requires deployment or emulation with the Firebase CLI so that it can correctly understand out how to configure the function before deployment.
I suggest following along with the Firebase getting started guide for Cloud Functions to walk through an example. You will see that step 2 requires you to install the CLI.
If you must use the Google Cloud console, you simply cannot use the firebase-functions module to declare your function. Instead, maybe go through this tutorial and use the @google-cloud/functions-framework module. You will not be able to use this to easily build a Firebase callable type function, however.