【问题标题】:how to test firebase onCall() locally?如何在本地测试firebase onCall()?
【发布时间】:2018-11-17 15:50:40
【问题描述】:

我正在尝试使用 firebase 作为我的 android 应用程序的后端。 但是在这个早期阶段,部署到谷歌服务器并测试每个新功能和每个修复都太慢了。

我想知道是否可以在本地运行它,以便我的 android 应用程序可以在 localhost:port 上调用 onCall() 函数?

我找到了一些 .useFunctionsEmulator('http://localhost:8080'); 但我不确定我需要什么导入以及如何启动本地“服务器”

【问题讨论】:

  • 目前不支持。您必须部署该功能或使用 CLI 在模拟器中对其进行测试。
  • 道格,你有什么模拟器?请给我一些链接,我可以在其中阅读有关 firebase 模拟器和在其中运行 firebase 应用程序的信息?

标签: javascript firebase google-cloud-functions


【解决方案1】:

当您启动 Firebase 模拟器时 firebase emulators:start 检查日志消息以查看您的云功能在哪里运行,例如functions: Emulator started at http://localhost:5001。 您可以使用 firebase.functions().useFunctionsEmulator('http://localhost:5001')

访问然后调用您的函数

如果您在 真实设备 上进行测试,则必须将 localhost 替换为 您的 PC 的 IP 地址,假设 PC + 设备连接到同一网络。

如果您在 Android Studio 的设备模拟器上进行测试,则必须更换 带有 10.0.2.2 的 localhost,因为 localhost 会将模拟器指向它自己的 本地主机地址,而不是您的 PC。

这对我有用。

【讨论】:

    【解决方案2】:

    我刚刚测试过,我是否遇到过同样的问题,但我没有找到解决方法,因为我猜是使用过时的信息。

    那么,我通过我的 REACT 应用程序做什么

    我在使用 config 初始化 firebase 应用程序后添加了这一行:

    app.functions().useFunctionsEmulator('http://localhost:5000')
    

    我使用命令“firebase serve”来启动本地模拟过程。

    请注意,5000 是默认端口,您可以通过添加 --port xxxx 来更改它,其中 xxxx 是端口号。

    对于 Android,该功能显然在文档中,所以我想它也应该可以工作:https://developers.google.com/android/reference/com/google/firebase/functions/FirebaseFunctions

    那么,如果您要在设备上进行测试,即不是您的开发机器,那么它将不是 localhost,而是您机器的地址,或者您可以使用 ngrok 重定向端口并使用它们为您提供的临时链接。

    【讨论】:

    • Simon,目前我正在像这样初始化我的 firebase 应用程序:functions = require('firebase-functions'); admin = require('firebase-admin'); const app = admin.initializeApp();而且app下没有这个方法。
    • 既然你声明了 functions = require ... 该方法将在函数上。
    • @SimonCadieux 你能更清楚地解释一下吗?我有同样的问题
    • 我身上还存在,有什么解决办法吗?
    【解决方案3】:

    您正在寻找:

      firebase.initializeApp(config);
      firebase.functions().useFunctionsEmulator('http://localhost:5001')
    

    'http://localhost:5001' 更改为您的模拟器设置为运行的任何内容

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 2018-06-21
      • 2021-05-18
      • 2018-11-25
      • 2021-11-30
      相关资源
      最近更新 更多