简体   繁体   English

如何在本地测试 firebase onCall()?

[英]how to test firebase onCall() locally?

I'm trying to use firebase as backend of my android application.我正在尝试使用 firebase 作为我的 android 应用程序的后端。 But at this early stage it's too slow to deploy to google server and test every new functionality and every fix.但是在这个早期阶段,部署到谷歌服务器并测试每个新功能和每个修复程序太慢了。

I wonder is it possible to run it locally so my android app can call onCall() functions on localhost:port ?我想知道是否可以在本地运行它,以便我的 android 应用程序可以在 localhost:port 上调用 onCall() 函数?

I've found some .useFunctionsEmulator(' http://localhost:8080 ');我发现了一些 .useFunctionsEmulator(' http://localhost:8080 '); but I'm not sure what import do I need and how exactly to start local "server"但我不确定我需要什么导入以及如何启动本地“服务器”

When you start your Firebase Emulators with firebase emulators:start check the log messages to see where your Cloud Functions are running, eg functions: Emulator started at http://localhost:5001 .当您使用firebase emulators:start Firebase Emulators 时,请检查日志消息以查看您的 Cloud Functions 在哪里运行,例如functions: Emulator started at http://localhost:5001 You can access then call your function by using firebase.functions().useFunctionsEmulator('http://localhost:5001')您可以使用firebase.functions().useFunctionsEmulator('http://localhost:5001')访问然后调用您的函数

If you are testing on a real device you will have to replace localhost with the IP address of your PC , assuming that PC + device are connected to the same network.如果您在真实设备上进行测试,则必须将 localhost 替换为您 PCIP 地址,假设 PC + 设备连接到同一网络。

If you are testing on Android Studio's device Emulator you will have to replace localhost with 10.0.2.2 , since localhost will point the Emulator to its own localhost address and not your PC's.如果您在Android Studio 的设备模拟器上进行测试,则必须将 localhost 替换为10.0.2.2 ,因为 localhost 会将模拟器指向其自己的本地主机地址,而不是您的 PC。

This did the trick for me.这对我有用。

I just tested it has i had the same problem and had not found how to solve it because i had ran on outdated information i guess.我刚刚测试了它,我遇到了同样的问题,但没有找到解决方法,因为我猜我使用了过时的信息。

So, what i do from my REACT app所以,我从我的 REACT 应用程序中做了什么

i added this line after initializing the firebase app with config:我在使用配置初始化 firebase 应用程序后添加了这一行:

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

and i use the command "firebase serve" to start the local emulating process.我使用命令“firebase serve”来启动本地模拟过程。

Note that 5000 is the default port you can change that by adding --port xxxx where xxxx is the port number.请注意,5000 是默认端口,您可以通过添加 --port xxxx 来更改它,其中 xxxx 是端口号。

For Android the function is clearly in the doc so i guess it should work too: https://developers.google.com/android/reference/com/google/firebase/functions/FirebaseFunctions对于 Android,该功能显然在文档中,所以我想它也应该可以工作: https : //developers.google.com/android/reference/com/google/firebase/functions/FirebaseFunctions

then if you would be testing on the device, meaning not your dev machine then it would not be localhost but the adresse of your machine or you could use ngrok to redirect port and use the temporary link they provide you.那么如果您要在设备上进行测试,这意味着不是您的开发机器,那么它不会是本地主机,而是您机器的地址,或者您可以使用 ngrok 重定向端口并使用他们为您提供的临时链接。

You are looking for:您正在寻找:

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

Changing 'http://localhost:5001' to whatever your emulator is set to run on'http://localhost:5001'更改为您的模拟器设置为运行的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM