16
votes

I couldn't find a solution for this use case in Firebase official guides.

  • They are HTTPS callable functions
  • Want to run Functions locally using Cloud Functions shell to test
  • Functions save received data to Firestore
  • The 'auth' context information is also needed

My code as below. Thanks in advance.


Function :

exports.myFunction = functions.https.onCall((data, context) => {
  const id = context.auth.uid;
  const message = data.message;

  admin.firestore()...
  // Do something with Firestore //
});

Client call :

const message = { message: 'Hello.' };

firebase.functions().httpsCallable('myFunction')(message)
  .then(result => {
    // Do something //
  })
  .catch(error => {
    // Error handler //
  });
2
This is not possible today. The team is working on it. - Doug Stevenson
If your purpose is to test your code. I would suggest creating 2 firebase project (1) development which you can also do some integration testing (normally you would abstract the database calls with mocked data) and (2) production project where you deploy tested code. - Maciej Caputa
Thank you for your answers. - Peter Park
Is there a way to connect my client (using firebase web SDK) to the local server instead of using shell? - czphilip
@DougStevenson when will it be ready? - Saša Šijak

2 Answers

1
votes

There is an api exactly for this use case, see here.

I used it in javascript(Client side) as follows -

button.addEventListener('click',()=>{
//use locally deployed function
firebase.functions().useFunctionsEmulator('http://localhost:5001');
//get function reference
const sayHello = firebase.functions().httpsCallable('sayHello');
sayHello().then(result=>{
    console.log(result.data);
}) 
})

where sayHello() is the callable firebase function.

When the client is an android emulator/device. Use 10.0.2.2 in place of localhost.

Also the code for flutter would be like so -

CloudFunctions.instance.useFunctionsEmulator(origin: 'http://10.0.2.2:5000')
    .getHttpsCallable(functionName: 'sayHello')
0
votes

Cloud functions have emulators for that. Check this link it can suite your case. Its not functions shell, but for testing purposes i think it can still works for you