首页 > 解决方案 > Firebase 云功能模拟器可以使用生产 Firestore 调用触发器吗?

问题描述

我正在尝试在具有真实生产 Firestore 的模拟器上测试 Firebase 云功能。当我启动我的函数模拟器时,我收到一个错误i functions[sendPushOnNewMessage]:function ignored because the firestore emulator does not exist or is not running.有趣的是,我可以从 firestore(从生产数据)中读取,但 firebase 模拟器拒绝允许这些函数启动。

❯ firebase emulators:start --only functions
i  emulators: Starting emulators: functions
i  ui: Emulator UI logging to ui-debug.log
⚠  functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /Users/.../functions/3afae-a81e699d1cf0.json. Non-emulated services will access production using these credentials. Be careful!
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
>  Starting up...
i  functions[createUserRecord]: function ignored because the auth emulator does not exist or is not running.
i  functions[sendPushOnNewMessage]: function ignored because the firestore emulator does not exist or is not running.

这是我使用服务帐户进行设置的方式(我知道这是可行的,因为我可以阅读文档):

// Service account initialization.
let serviceAccount = require('./3afae-a81e699d1cf0.json');
var initOptions = {}
emulator#set_up_admin_credentials_optional.
if (process.env.FUNCTIONS_EMULATOR) {
  initOptions.credential = admin.credential.cert(serviceAccount)
}
admin.initializeApp(initOptions);
const firestore = admin.firestore();

我的触发功能示例:

exports.sendPushOnNewMessage = functions.firestore.document('chats/{chatID}/messages/{messageID}').onCreate((change, context) => {
    console.log("Trigger called")
}

标签: node.jsfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-cli

解决方案


推荐阅读