简体   繁体   中英

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. 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? .

However, when I run firebase emulator:start and console.log(firebase.functions().useFunctionsEmulator(' http://localhost:5001 '), it just showed undefined.

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.

I got the emulators working and handling local requests by calling the useFunctionsEmulator() method just after initializing the firebase app in the client. Calling it prior caused errors.

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

useFunctionsEmulator() doesn't return anything, it's just a 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:

I switch my onCall function to an onRequest function like so:

// 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. When I'm done editing the function I switch it back to an onCall function. I hope this helps!

As @app_ wrote, but also this may be worth to someone:

If you use regions, they should only be set for the online call, not the emulated one. This works for me (JavaScript client):

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. Please let me know if there's a standard way to sniff that, from the Firebase client.

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

For having troubles calling

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

You can test your onCall method in your app by adding this lines.

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

For Firebase 9 (modular) , use connectFunctionsEmulator instead:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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