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 函数)?
这是不可能的。Cloud Functions 触发器只能在响应部署它们的项目的资源更改时触发。这适用于所有类型的触发器,包括 Firestore。
如果您希望代码运行以响应另一个项目中的更改,则必须将该功能部署到该项目。
| 归档时间: |
|
| 查看次数: |
625 次 |
| 最近记录: |