3

According to Firebase's documentation, the following code can be used to call a onCall function named addMessage.

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
    // Read result of the Cloud Function.
    var sanitizedMessage = result.data.text;
})

I have a function named test, with the following code in Javascript (just to test this functionality):

exports.test = functions.https.onCall((data, context) => {
  console.log(data);
  data.page++;
  console.log(data);

  var testing = firebase.functions().httpsCallable('test');

  while(data.page < 5) {
    testing({page: data.page}).then(res => {
    console.log("done");
    })
  }
});

When running this, however, I get the following error:

Unhandled error TypeError: firebase.functions is not a function

What am I doing wrong?

Christiaan Louw
  • 107
  • 2
  • 11
  • possible duplicate of https://stackoverflow.com/questions/55883213/typeerror-firebase-functions-is-not-a-function – Travis Webb Nov 06 '19 at 19:56
  • Here's a similar question for you to refer https://stackoverflow.com/questions/49480761/cloud-functions-with-firebase-firebase-functions-is-not-a-function – Phani Mahesh Nov 06 '19 at 20:00
  • @TravisWebb That is not a duplicate. The error may be the same, but the question you linked is trying to run a node script in the browser. OP here is trying to **call** a Cloud Function from within the browser, in which case they need to include the client-side `firebase-functions` SDK. – Frank van Puffelen Nov 06 '19 at 20:09

3 Answers3

3

firebase.functions() method comes from firebase/functions package, not from firebase or firebase-functions.

const firebase = require('firebase/app');
require('firebase/functions');
const firebaseConfig = {<YOUR_CONFIG_HERE>};
const app = firebase.initializeApp(firebaseConfig);
const functions = app.functions();
Michał Dziwota
  • 838
  • 7
  • 16
  • I did exactly this now, but when deploying I get the error **TypeError: app.functions is not a function** – Christiaan Louw Nov 07 '19 at 13:46
  • So my issue ended up being something with npm. I deleted my package-lock.json and node_modules folder and reinstalled everything. After that, your answer worked! Thanks a lot – Christiaan Louw Nov 07 '19 at 14:33
0

You seem to not be including the client-side Cloud Functions SDK into your web app. Depending on how you include the other Firebase SDKs, you might need to do what Phani linked to, or include it in another (e.g. <script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-functions.js"></script>).

The process is covered in the documentation on setting up your client development environment.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have included both firebase and firebase-functions as follows: `const firebase = require("firebase"); const functions = require('firebase-functions');` I've also tried `firebase.functions().httpsCallable('test');` As well as `functions.httpsCallable('test');` Always the same error. – Christiaan Louw Nov 07 '19 at 10:04
  • I should also mention that this code is all inside my index.js file in the functions directory of the firebase project. As far as I know, this is not a "web-app" or anything, it's just the functions of the project in node.js. – Christiaan Louw Nov 07 '19 at 10:55
  • If you're trying to call one firebase function from another one, you're probably over complicating things. See this answer: https://stackoverflow.com/a/42787159/3399890 – robsiemb Nov 07 '19 at 14:02
  • @robsiemb I have the same issue. If you want to implement your system following a microservices approach, the general idea is to divide the system into services and expose an API. In my case, I also don't want all the APIs to de directly usable by client and must connect them through an intermediary service. In such cases, I need to use an HTTPS callable trigger. So, can this be done by using the web SDK from Nodejs? – Abrar Hossain Feb 17 '20 at 18:35
0

Try adding scripts definitions to app.js

scriptInit = (scrName) => {
    const script = document.createElement("script");

    script.src = scrName;
    script.async = false;
    script.type="module"

    document.body.appendChild(script);
}

componentWillMount() {
    this.scriptInit("https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js");
    this.scriptInit("https://www.gstatic.com/firebasejs/9.1.3/firebase-functions.js");
}

for function call the following worked:

const firebase = require("firebase/app");
const functions = require("firebase/functions");
const firebaseConfig = {FIREBASE_CONFIG};
const app = firebase.initializeApp(FIREBASE_CONFIG);
const functionsFB = functions.getFunctions(app);

return (async () => {
let fbSomeFuncName =  (functions.httpsCallable(functionsFB, 'somefunction'))
            let data = await fbSomeFuncName().then(data=>{return (data.data)}).catch(err=>{console.log("error:"+err)})
return data
// don't forget to add data to the response 
nima
  • 7,796
  • 12
  • 36
  • 53