我正在尝试在本地Firebase模拟器上的辅助数据库实例上运行实时数据库触发器。我遵循以下示例:
通过在instance(dbInstanceId)
之后指定functions.database
。
我的函数的 package.json :
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.0.0",
"firebase-functions": "^3.8.0"
},
Firebase版本:
$ firebase --version
8.6.0
lib / index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const defaultDb = admin.database();
// default instance name is 'sgo-personal'
export const onStaffCreatedDefault = functions.database
.ref('/staff/{name}')
.onCreate(async (snapshot, context) => {
defaultDb.ref('/meta/defaultDbCount').set(admin.database.ServerValue.increment(1));
console.log("Hello from default instance DB");
return null;
});
export const onStaffCreatedDb1 = functions.database.instance('db1')
.ref('/staff/{name}')
.onCreate(async (snapshot, context) => {
defaultDb.ref('/meta/db1Count').set(admin.database.ServerValue.increment(1));
console.log("Hello from DB1");
return null;
});
export const onStaffCreatedDb2 = functions.database.instance('db2')
.ref('/staff/{name}')
.onCreate(async (snapshot, context) => {
defaultDb.ref('/meta/db2Count').set(admin.database.ServerValue.increment(1));
console.log("Hello from DB2");
return null;
});
问题:当我在辅助数据库实例上添加数据时,它不会触发任何操作。这是本地模拟器的限制吗?
如果我在db1
或db2
实例上添加人员,则不会打印日志。
但是,如果我在默认实例中添加人员,则所有会根据日志触发云功能:
i functions: Beginning execution of "onStaffCreatedDefault"
i functions: Beginning execution of "onStaffCreatedDb2"
> Hello from default instance DB
i functions: Finished "onStaffCreatedDefault" in ~1s
i functions: Beginning execution of "onStaffCreatedDb1"
i functions: Finished "onStaffCreatedDb1" in ~1s
> Hello from DB2
> Hello from DB1
i functions: Finished "onStaffCreatedDb2" in ~1s
附加的屏幕截图:Firebase Emulator UI
我希望在/sgo-personal/meta/db2Count
中添加2条记录后,2
将是db2
。