5

I have a function that I call from the clientside currently, and now I also need to call it from Firebase Cloud Functions.

The syntax is for the function I need to call is

exports.querySomeAPI = functions.https.onCall((data)=>{
    //Does work and returns stuff
});

And I call it from the clientside with

var querySomeAPI = firebase.functions().httpsCallable('querySomeAPI');
querySomeAPI({
    data: "data"
}).then(response => {console.log("Query Response is: ", response);});

Since firebase is not defined on my backend, I tried to call it from the serverside with

var querySomeAPI = admin.functions().httpsCallable('querySomeAPI');
querySomeAPI({
    data: "data"
}).then(response => {console.log("Query Response is: ", response);});

and found out that admin.functions() does not exist. So I tried to call it as a normal function with

querySomeAPI({
    data: "data"
}).then(response => {console.log("Query Response is: ", response);});

as well as a few other methods to no avail. I know there has to be a way to call an exported function from within Firebase Functions but none of the methods I've tried so far have worked.

Anyone know how this can be done?

Link for how to call the https callable function on the clientside

Devin Carpenter
  • 907
  • 8
  • 21

1 Answers1

3

You're in for a lot of trouble (and unnecessarily larger billing) if you try to go about calling a function directly from another function like this. You're far better off just creating a plain old JS function that both of your Cloud Functions exports can share independently of each other.

See this: Calling a Cloud Function from another Cloud Function

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 5
    I disagree with this answer. While it may be sufficient in many cases to pull the desired function out into a shared library, remotely calling "function to function" is basically the *entire* idea behind microservices and there are often very good reasons to do this. For example, if your common functionality has very different resource needs (e.g. memory) it doesn't necessarily make sense for all clients that need to call that code to need to max out their memory, too. – adevine Apr 24 '19 at 04:02
  • 1
    @adevine My observation is that *most of the time* mobile developers using callable functions are not actually trying to compose microservices. This is not even how callable type functions are intended to be used. But if you have a specific solution that's helpful here, please add your answer here so others can benefit. – Doug Stevenson Apr 24 '19 at 05:16
  • I'd say this can be useful for working with very large sets of data which could cause timeout or memory issues independently. If I want to split the load, I need to do that through a new http request. – Kevin Robinson Feb 23 '21 at 13:30