Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 internet about this, I think that's because this is alpha, so Please help me on this.

like image 878
BLUE Avatar asked Aug 28 '19 03:08

BLUE


3 Answers

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!

like image 75
Supra_01 Avatar answered Nov 17 '22 08:11

Supra_01


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");
like image 38
app_ Avatar answered Nov 17 '22 08:11

app_


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");
like image 7
MathStims Avatar answered Nov 17 '22 09:11

MathStims