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

时间:2018-11-17 15:50:40

标签: javascript firebase google-cloud-functions

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

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

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

3 个答案:

答案 0 :(得分:3)

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

进行访问,然后调用函数

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

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

这帮了我大忙。

答案 1 :(得分:0)

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

那么,我在我的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

然后,如果您要在设备上进行测试,这意味着不是您的dev机器,则不是本地主机,而是机器的地址,或者您可以使用ngrok重定向端口并使用它们为您提供的临时链接。

答案 2 :(得分:0)

您正在寻找:

if form.is_valid():
    image = form.save() # this creates the model, the upload paths are created here!
    image.patient = patient
    image.enhanced.save("enhanced_" + image.original.name, main(image.original.path)) # I made this up to avoid conflicts in your storage
    image.segmented.save("segmented_" + image.original.name, segment(image.enhanced.path))
    image.save() # this updates the model with the new images
    messages.success(request,"Image added successfully!") # I moved this here, more accurate, in case save fails....
    return HttpResponseRedirect(reverse_lazy('patients:patient_detail', kwargs={'pk' : image.patient.pk}))

firebase.initializeApp(config); firebase.functions().useFunctionsEmulator('http://localhost:5001') 更改为您要在其上运行的模拟器

相关问题