Kermit
Kermit

Reputation: 3417

Google Cloud Functions v2 - onValueUpdated and onValueWritten never triggers (error)

I have deployed a v2 Google Cloud Function implementing the onValueUpdated trigger.

I have followed this guide --> https://firebase.google.com/docs/functions/get-started?gen=2nd

This is my code:

index.js

exports.testEmailSend = require("./src/manual-tasks/testBench").testEmailSend;
exports.triggerCacheRebuild = require("./src/cache-tasks/cache").triggerCacheRebuild;

cache.js

const { onValueUpdated } = require("firebase-functions/v2/database");
const database = require("../firebase-db");
const { logger } = require("firebase-functions");

exports.triggerCacheRebuild = onValueUpdated({
  ref: "/cache/ganttJson/trigger",
  region: "europe-west1",
}), async (event) => {
  logger.info("triggerCacheRebuild");
  const now = new Date().getTime();
  await database.ref("/cache/ganttJson/timeStamp").set(now);

  return Promise.resolve();
};

firebase-db

const admin = require("firebase-admin");

const app = admin.initializeApp();
const database = admin.database(app);
module.exports = database;

testBench.js

const { onValueCreated } = require("firebase-functions/v2/database");

const funcEmailBuilder = require("../sendinblue/testBench");

exports.testEmailSend = onValueCreated(
  {
    ref: "/DUMMY/123",
    // instance: "default",
    region: "europe-west1",
  },
  async (event) => {
    await funcEmailBuilder.sendAlertEmail();
    return Promise.resolve();
  },
);

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "20"
  },
  "main": "index.js",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "firebase-admin": "^12.1.0",
    "firebase-functions": "^4.9.0",
    "node-fetch": "^3.3.2",
    "sib-api-v3-sdk": "^8.5.0",
    "url": "^0.11.3"
  },
  "devDependencies": {
    "eslint": "^8.15.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-promise": "^6.1.1",
    "eslint-plugin-snakecasejs": "^2.2.0",
    "firebase-functions-test": "^3.1.0"
  },
  "private": true
}

When updating the Realtime Database node in /cache/hello/trigger, the code does not run. I get error:

TypeError: func is not a function
    at /workspace/node_modules/firebase-functions/lib/common/onInit.js:33:16
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at /workspace/node_modules/firebase-functions/lib/v2/trace.js:18:37
    at func (/workspace/node_modules/firebase-functions/lib/v2/providers/database.js:187:78)
    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)

EDIT I am experiencing the exact same problem with the onValueWritten trigger. The only trigger I have tested that works is the onValueCreated trigger.

A clear description of how the trigger is supposed to be used would help a lot! The error message gives me no clue to what is wrong.

EDIT 2 Just to be clear: I have not declared any function called "func" - this must be a part of the firebase code. I am using version 13.7.4 of firebase-tools and Node.js v20.12.2

What is wrong?

Upvotes: 0

Views: 450

Answers (1)

Kermit
Kermit

Reputation: 3417

Ok, so after speaking to Firebase Support (many thanks to the fast and friendly support!) I found out the problem!

The given code:

exports.triggerCacheRebuild = onValueUpdated({
  ref: "/cache/ganttJson/trigger",
  region: "europe-west1",
}), async (event) => {
  logger.info("triggerCacheRebuild");
  const now = new Date().getTime();
  await database.ref("/cache/ganttJson/timeStamp").set(now);

  return Promise.resolve();
};

Should actually read:

exports.triggerCacheRebuild = onValueUpdated({
  ref: "/cache/ganttJson/trigger",
  region: "europe-west1",
}, async (event) => {
  logger.info("triggerCacheRebuild");
  const now = new Date().getTime();
  await database.ref("/cache/ganttJson/timeStamp").set(now);

  return Promise.resolve();
});

What differs is the closing parentheses on the fourth row! That is why Firebase says that "func is not a function".

Upvotes: 1

Related Questions