[英]Google firebase database with google functions running locally
I have an app connected to google firebase realtime database.我有一个连接到 google firebase 实时数据库的应用程序。 I want to test google cloud functions as a server for running backend functions triggered once every day or triggered by data changed in the database.
我想测试谷歌云功能作为运行后端功能的服务器,每天触发一次或由数据库中的数据更改触发。
I have an emulator instance running locally with a typescript function that I wrote.我有一个模拟器实例在本地运行,带有我编写的打字稿函数。 I have the realtime database running, connected to the app and working well.
我有实时数据库在运行,连接到应用程序并且运行良好。
Now when I change data in the database, the functions are not being triggered.现在,当我更改数据库中的数据时,不会触发函数。
function in index.ts: index.ts 中的函数:
import * as functions from "firebase-functions";
exports.simpleDbFunction = functions.region('europe-west1').database.ref('/some path in the database')
.onCreate((snap, context) => {
console.log("functions triggered by datachange:", snap.val())
});
the firebase project is connected to the correct project (using the interface in the terminal). firebase 项目已连接到正确的项目(使用终端中的界面)。 the app and realtime database are currently working and I can see changes in the data inside the firebase console.
该应用程序和实时数据库目前正在运行,我可以在 firebase 控制台中看到数据的变化。
What am I missing?我错过了什么?
I've also tried the more complicated example from google's documents (makeUppercase function).我还尝试了谷歌文档中更复杂的示例(makeUppercase 函数)。 But when it didn't work I went back to a simple function that is triggered on data creation and prints to the console.
但是当它不起作用时,我回到了一个简单的函数,该函数在数据创建时触发并打印到控制台。
Edit: I notice that in the emulator UI the real time database is empty.编辑:我注意到在模拟器 UI 中实时数据库是空的。 Shouldn't it connect to the real time database of the app?
它不应该连接到应用程序的实时数据库吗? isn't that why I connected the emulator to a specific project?
这不是我将模拟器连接到特定项目的原因吗? Edit2: I tried changing the function line to:
Edit2:我尝试将功能行更改为:
functions.region('europe-west1').database.instance("the URL of the rtdb").ref(...
functions.region('europe-west1').database.instance("rtdb 的 URL").ref(...
and I get an error saying that the event function has malformed resource member我收到一条错误消息,指出事件函数的资源成员格式错误
So I kinda solved it:所以我有点解决了它:
I found out that you can't really connect from a local emulator to the production environment of your actual google firebase.我发现您无法真正从本地模拟器连接到实际 google firebase 的生产环境。 (Which is kinda weird, you have to commit to this technology even before you had a chance to see any code in action) <-- fix me if I'm wrong about this
(这有点奇怪,你甚至在有机会看到任何代码运行之前就必须致力于这项技术) <--如果我对此有误,请修复我
If you use realtime database cloud functions you can't use any region other than 'us-central1'.如果您使用实时数据库云功能,则不能使用“us-central1”以外的任何区域。 as mentioned in this document: https://firebase.google.com/docs/functions/locations
如本文档中所述: https ://firebase.google.com/docs/functions/locations
You can connect your app to the local emulator on your machine: Get your database instance:您可以将您的应用程序连接到您机器上的本地模拟器:获取您的数据库实例:
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseDatabase 数据库 = FirebaseDatabase.getInstance();
Add the following line in your app (which is weird) before the first database operation:在第一次数据库操作之前,在您的应用程序中添加以下行(这很奇怪):
database.useEmulator("10.0.2.2", 9004);
database.useEmulator("10.0.2.2", 9004);
change 9004 to your local realtime database port.将 9004 更改为您本地的实时数据库端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.