是否可以通过更改位于单独 Firebase 项目中的 Firestore 来触发 Firebase 函数?

Jas*_*ark 2 node.js firebase google-cloud-functions google-cloud-firestore

假设我有一个名为“A”的 Firebase 项目。在这个项目中,我有一个 Cloud Firestore 触发的 Firebase 函数,当 Firestore 中的文档发生更改时需要运行该函数。默认情况下,Firebase 函数将侦听项目 A 上 Firestore 中的更改。

但是,假设我有一个特定的用例,其中有一个名为“B”的 Firebase 项目。我需要在项目 B 中的 Firestore 发生的 Firestore 更改时触发项目 A 中的 Firebase 函数。

这可能吗?Firebase 文档确实显示了初始化多个项目,这将允许我连接到多个数据库,如下所示:

const admin = require("firebase-admin");
const serviceAccount = require("path/to/serviceAccountKey.json");

const secondaryAppConfig = {
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
};

// Initialize another app with a different config
const secondary = firebase.initializeApp(secondaryAppConfig, "secondary");

// Retrieve the database.
const secondaryDatabase = secondary.database();
Run Code Online (Sandbox Code Playgroud)

但这不允许我在我的辅助项目上触发 Firestore Triggered Firebase 函数。Firebase 函数firebase-functions直接调用方法,而调用数据库调用初始化的项目。

const functions = require('firebase-functions');

exports.myFunction = functions.firestore
  .document('...')
  .onWrite((change, context) => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

我想做的可能吗?或者有没有人有解决方法(除了在项目 B 中创建这个 Firebase 函数)?

Dou*_*son 5

这是不可能的。Cloud Functions 触发器只能在响应部署它们的项目的资源更改时触发。这适用于所有类型的触发器,包括 Firestore。

如果您希望代码运行以响应另一个项目中的更改,则必须将该功能部署到该项目。

  • 据我所知,没有任何文档,但我曾经在 Google 的 Cloud Functions 工作过,所以我 100% 确定情况确实如此。如果您想直接听到 Firebase 的消息,您应该联系 Firebase 支持人员。https://support.google.com/firebase/contact/support (4认同)