首页 > 解决方案 > 如何确保 Web 应用程序使用具有正确 URL 的 firebase 函数模拟器进行测试

问题描述

我正在尝试测试 google firebase oncall 功能,因此创建了一个带有 google 身份验证的基本 vue 应用程序。我添加了以下行以确保使用了模拟器并且可以在本地进行测试:

firebase.functions().useEmulator("localhost", 5001);

我在应用程序和我拥有的功能中有 firebase 8.2.0:

"firebase-admin": "^8.10.0"
"firebase-functions": "^3.12.0"

该应用程序使用以下代码调用该函数:

  var callFunction = firebase
    .functions()
    .httpsCallable("myFunction");
  callFunction({
    dataItem: "xxxxxxxxxxxxxxxx"
  })
    .then(result => {
      console.log(result);
    })
    .catch(error => {
      console.log(error);
    });

在应用程序中触发该功能时,会产生以下错误:

index.esm.js?7dc7:490 POST http://http//localhost:5001:undefined/myProject/us-central1/myFunction net::ERR_NAME_NOT_RESOLVED

模拟器提供的实际网址:

http://localhost:5001/myProject/us-central1/myFunction.myFunction

这显然是行不通的。如何清理此 URL?

标签: javascriptfirebasegoogle-cloud-platformgoogle-cloud-functionsfirebase-cli

解决方案


首先,确保初始化应用程序客户端,并尝试useFunctionsEmulator:

const app = window.firebase.initializeApp(config)
app.functions().useFunctionsEmulator("http://localhost:5001");

推荐阅读