简体   繁体   English

具有本地运行的谷歌功能的谷歌 firebase 数据库

[英]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:所以我有点解决了它:

  1. 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 (这有点奇怪,你甚至在有机会看到任何代码运行之前就必须致力于这项技术) <--如果我对此有误,请修复我

  2. 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

  3. 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.

相关问题 使用 Google Kubernetes 引擎在本地运行项目时,VS Code 使用了错误的 Google Cloud 帐户 - VS Code using wrong Google Cloud account when running project locally with Google Kubernetes Engine Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists Firebase 实时数据库:Google 警告写入规则 - Firebase Realtime Database: Google warning for write rules Firebase/谷歌云功能基于时间的触发器防止并发执行 - Firebase/google cloud functions time based trigger prevent concurrent executions Flutter Firebase 在 Google Play 中发布后数据库无法正常工作 - Flutter Firebase Database not working on release after publish in Google play 从 Java 中的云函数访问 Google Firebase 数据库中的数据 - Access data in Google Firebase database from a cloud function in Java 谷歌云功能 Firebase 使用 NodeJS 更新 FieldValue.increment(1) - TypeError: FieldValue.increment is not a function - Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function 从连接到 Google Functions 的端点调用时,Firebase ID 令牌具有不正确的“aud”(受众)声明 - Firebase ID token has incorrect "aud" (audience) claim when calling from Endpoint connected to Google Functions 当我尝试初始化 firebase 应用程序时,为什么 Google Cloud Functions 会抛出“配置 firebase.databaseURL 的无效值”错误? - Why is Google Cloud Functions throwing a "Invalid value for config firebase.databaseURL" error when I try to initialize a firebase app? 并行运行 Google Workflows - Running Google Workflows in Parallel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM