Google Cloud Functions v2 - onValueUpdated 和 onValueWritten 永远不会触发(错误)

问题描述 投票:0回答:1

我已经部署了 v2 Google Cloud Function 来实现 onValueUpdated 触发器。

我已遵循本指南 --> https://firebase.google.com/docs/functions/get-started?gen=2nd

这是我的代码:

index.js

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

缓存.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
}

更新 /cache/hello/trigger 中的实时数据库节点时,代码不运行。我收到错误:

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)

编辑 我在使用

onValueWritten
触发器时遇到了完全相同的问题。我测试过的唯一有效的触发器是
onValueCreated
触发器。

清楚地描述如何使用触发器将会有很大帮助!错误消息让我不知道出了什么问题。

编辑2 需要明确的是:我没有声明任何名为“func”的函数 - 这必须是 firebase 代码的一部分。我使用的是 firebase-tools 版本 13.7.4 和 Node.js v20.12.2

出了什么问题?

javascript firebase google-cloud-platform firebase-realtime-database google-cloud-functions
1个回答
0
投票

好的,在与 Firebase 支持人员交谈后(非常感谢快速友好的支持!)我发现了问题!

给定代码:

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();
};

实际上应该阅读:

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 说“func 不是函数”。

© www.soinside.com 2019 - 2024. All rights reserved.