【问题标题】:Firebase emulator for Cloud Functions not updating the codeCloud Functions 的 Firebase 模拟器未更新代码
【发布时间】:2021-01-31 15:45:14
【问题描述】:

我正在使用firebase deploy --only functions 部署以下云功能:

export const testFunction = functions.https.onCall((data, context) => {
  return {"data": "hello"};
});

当使用代码从客户端应用程序调用它时

var testFunction = firebase.functions().httpsCallable("testFunction");
      testFunction().then((result) => {
        // Read result of the Cloud Function.
        this.data = result.data;
});

它按预期工作。 现在,我想继续开发在本地模拟器上测试它的功能,因此,按照文档,我将此行添加到 Web 应用程序代码中(在功能之前)

firebase.functions().useEmulator("localhost", 5001); // for local simulator

我运行本地模拟器:

firebase emulators:start --only functions

如果我现在运行客户端应用程序,我会正确地看到调用通过本地模拟器而不是远程云功能。

问题:如果修改代码,本地函数不会更新。我需要再次运行 firebase deploy 才能看到响应的变化。如何只在本地部署?

【问题讨论】:

  • 根据Official Documentation,代码会在模拟器上自动更新,但如果需要转译代码,您需要再次启动模拟器。你使用的是 TypeScript 还是 React?

标签: firebase google-cloud-functions


【解决方案1】:

您可以使用本文档here 中描述的函数外壳,以便在本地部署、运行和测试函数。我觉得文档很混乱,因为它只提到了如何使用 .sh 脚本在本地进行测试。

firebase functions:shell

注意,如果你使用shell,你不需要单独运行模拟器; shell 将为您运行它们。 shell 将根据 index.js 函数文件的更改自动更新,但如果您使用的是 TypeScript,您可能需要在进行更改后重新构建。这通常通过进入函数目录并运行npm run build 来完成。根据answer,您也可以自动执行此操作,但我还没有尝试过这种方法。

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2020-05-31
    • 2020-07-28
    • 2017-09-18
    • 2018-10-16
    • 2021-04-22
    相关资源
    最近更新 更多