Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: How to call an https.onCall function node.js?

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?

like image 820
Christiaan Louw Avatar asked May 31 '26 15:05

Christiaan Louw


1 Answers

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();
like image 94
Michał Dziwota Avatar answered Jun 02 '26 05:06

Michał Dziwota