简体   繁体   English

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

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

I am currently working on the way to test my cloud functions locally.我目前正在研究在本地测试我的云功能的方法。 I found several ways but using firebase emulator and useFunctionsEmulator() method seemed great.我找到了几种方法,但使用 firebase 模拟器和 useFunctionsEmulator() 方法似乎很棒。 At https://firebase.google.com/docs/functions/local-emulator , they didn't say about the method, but I found it on this url How to test `functions.https.onCall` firebase cloud functions locally?https://firebase.google.com/docs/functions/local-emulator ,他们没有说方法,但是我在这个 url How to test `functions.https.onCall` firebase cloud functions locally? . .

However, when I run firebase emulator:start and console.log(firebase.functions().useFunctionsEmulator(' http://localhost:5001 '), it just showed undefined.但是,当我运行 firebase emulator:start 和 console.log(firebase.functions().useFunctionsEmulator(' http://localhost:5001 ') 时,它只是显示未定义。

I tried several inputs on the origin but nothing changed.我在原点上尝试了几个输入,但没有任何改变。 There's so little information on the inte.net about this, I think that's because this is alpha, so Please help me on this. inte.net 上关于此的信息很少,我认为那是因为这是 alpha,所以请帮助我解决这个问题。

I got the emulators working and handling local requests by calling the useFunctionsEmulator() method just after initializing the firebase app in the client.在客户端初始化 firebase 应用程序后,我通过调用 useFunctionsEmulator() 方法使模拟器工作并处理本地请求。 Calling it prior caused errors.调用它之前会导致错误。

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

useFunctionsEmulator() doesn't return anything, it's just a setter. useFunctionsEmulator()不返回任何东西,它只是一个 setter。

Use it in the following way:按以下方式使用它:

firebase.initializeApp(config);

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

I haven't been able to get useFunctionsEmulator() either but I have a workaround:我也无法获得useFunctionsEmulator()但我有一个解决方法:

I switch my onCall function to an onRequest function like so:我将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
});

Then I can serve my function locally with this command firebase serve --only functions which will display a url that I can send requests to via curl , postman or my browser.然后我可以使用这个命令firebase serve --only functions功能在本地提供我的功能,该firebase serve --only functions将显示我可以通过curl 、邮递员或我的浏览器发送请求的url When I'm done editing the function I switch it back to an onCall function.当我完成对函数的编辑后,我将其切换回onCall函数。 I hope this helps!我希望这有帮助!

As @app_ wrote, but also this may be worth to someone:正如@app_ 所写,但这对某人来说可能是值得的:

If you use regions, they should only be set for the online call, not the emulated one.如果您使用区域,它们应该只为在线呼叫设置,而不是被模拟的。 This works for me (JavaScript client):这对我有用(JavaScript 客户端):

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

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

The LOCAL is set earlier to true if we know we are run against emulated back-end.如果我们知道我们是针对模拟后端运行的,那么LOCAL会更早地设置为true Please let me know if there's a standard way to sniff that, from the Firebase client.请让我知道是否有从 Firebase 客户端嗅探它的标准方法。

For developer experience, it would be best if regions are handled the same way locally, meaning one doesn't need the above ternary operator.对于开发人员的体验,最好在本地以相同的方式处理区域,这意味着不需要上述三元运算符。

firebase-tools 8.6.0, JavaScript client 7.16.1 firebase-tools 8.6.0,JavaScript 客户端 7.16.1

For having troubles calling打电话有问题

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

You can test your onCall method in your app by adding this lines.您可以通过添加此行在您的应用程序中测试您的 onCall 方法。

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

or或者

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

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

For Firebase 9 (modular) , use connectFunctionsEmulator instead:对于Firebase 9 (modular) ,请改用connectFunctionsEmulator

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

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

If you have problem using Firebase 9, follow this guide .如果您在使用 Firebase 9 时遇到问题,请按照本指南进行操作。

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

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