繁体   English   中英

firebase 函数模拟器 useFunctionsEmulator() 方法不工作

[英]firebase functions emulator useFunctionsEmulator() method not working

我目前正在研究在本地测试我的云功能的方法。 我找到了几种方法,但使用 firebase 模拟器和 useFunctionsEmulator() 方法似乎很棒。 https://firebase.google.com/docs/functions/local-emulator ,他们没有说方法,但是我在这个 url How to test `functions.https.onCall` firebase cloud functions locally? .

但是,当我运行 firebase emulator:start 和 console.log(firebase.functions().useFunctionsEmulator(' http://localhost:5001 ') 时,它只是显示未定义。

我在原点上尝试了几个输入,但没有任何改变。 inte.net 上关于此的信息很少,我认为那是因为这是 alpha,所以请帮助我解决这个问题。

在客户端初始化 firebase 应用程序后,我通过调用 useFunctionsEmulator() 方法使模拟器工作并处理本地请求。 调用它之前会导致错误。

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

useFunctionsEmulator()不返回任何东西,它只是一个 setter。

按以下方式使用它:

firebase.initializeApp(config);

const functions = firebase.functions();
functions.useFunctionsEmulator("http://localhost:5001");

我也无法获得useFunctionsEmulator()但我有一个解决方法:

我将onCall函数切换为onRequest函数,如下所示:

// FROM THIS
exports.exampleFunction = functions.https.onCall((data, context) => {
  // CODE FOR CLOUD FUNCTION
});
// TO THIS
exports.exampleFunction = functions.https.onRequest((request, response) => {
  // CODE FOR CLOUD FUNCTION
});

然后我可以使用这个命令firebase serve --only functions功能在本地提供我的功能,该firebase serve --only functions将显示我可以通过curl 、邮递员或我的浏览器发送请求的url 当我完成对函数的编辑后,我将其切换回onCall函数。 我希望这有帮助!

正如@app_ 所写,但这对某人来说可能是值得的:

如果您使用区域,它们应该只为在线呼叫设置,而不是被模拟的。 这对我有用(JavaScript 客户端):

const fns = LOCAL ? firebase.app().functions() :
  firebase.app().functions(functionsRegion);

const log = fns.httpsCallable('logs_v200719');

如果我们知道我们是针对模拟后端运行的,那么LOCAL会更早地设置为true 请让我知道是否有从 Firebase 客户端嗅探它的标准方法。

对于开发人员的体验,最好在本地以相同的方式处理区域,这意味着不需要上述三元运算符。

firebase-tools 8.6.0,JavaScript 客户端 7.16.1

打电话有问题

firebase.functions().useFunctionsEmulator("http://localhost:5001");

您可以通过添加此行在您的应用程序中测试您的 onCall 方法。

FirebaseFunctions functions = FirebaseFunctions.getInstance();
        functions.useEmulator("10.0.2.2", 5001);

或者

functions.UseFunctionsEmulator("http://localhost:5004");

这是我的来源: https : //firebase.google.com/docs/functions/local-emulator

对于Firebase 9 (modular) ,请改用connectFunctionsEmulator

import { getApp } from "firebase/app";
import { getFunctions, connectFunctionsEmulator } from "firebase/functions";

const functions = getFunctions(getApp());
connectFunctionsEmulator(functions, "localhost", 5001);

如果您在使用 Firebase 9 时遇到问题,请按照本指南进行操作。

暂无
暂无

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

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