假设我有一个名为“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'); …Run Code Online (Sandbox Code Playgroud) node.js firebase google-cloud-functions google-cloud-firestore