Christiaan Louw
Christiaan Louw

Reputation: 117

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?

Upvotes: 3

Views: 3217

Answers (3)

Boris Mikhailovski
Boris Mikhailovski

Reputation: 1

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 

Upvotes: 0

Michał Dziwota
Michał Dziwota

Reputation: 948

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();

Upvotes: 3

Frank van Puffelen
Frank van Puffelen

Reputation: 598837

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.

Upvotes: 0

Related Questions